C # Note (2)-type conversion

Source: Internet
Author: User
Pay attention to the following issues when using the convert class for type conversion:
Using system;
Struct phonebook
{
Public string name;
Public uint age;
Public String phone;
Public struct address
{
Public String city;
Public String Street;
Public uint no;
}
}

Class welcome
{
Static void main ()
{
Try
{
Phonebook M_1;
Console. writeline ("enter your name :");
M_1.name = console. Readline ();
Console. writeline ("Enter your age :");
M_1.age = convert. touint32 (console. Readline ());
Console. writeline ("Enter your phone number :");
M_1.phone = Console. ReadLine ();

Console. WriteLine ("Enter your address :");
PhoneBook. address ad;
Console. WriteLine ("Enter your city :");
Ad. city = Console. ReadLine ();
/* M_1.address.city = Console. ReadLine (); * // you cannot directly access the members of the struct address in this way.
Console. WriteLine ("Enter your street :");
Ad. street = Console. ReadLine ();
Console. WriteLine ("enter your house number :");
Ad. no = Convert. ToUInt32 (Console. ReadLine ());

Console. WriteLine ("OK now ...... ");
Console. WriteLine ("---------------------------------------------------------------------------------");
Console. WriteLine ("your information is as follows: \ n ");
Console. WriteLine ("Name: {0}", M_1.name );
Console. WriteLine ("Age: {0}", M_1.age );
Console. WriteLine ("Tel: {0}", M_1.phone );
Console. WriteLine ("Address :");
}
Catch (FormatException e)
{
Console. WriteLine ("--------------------------------------------------------------------------------");
String msg = "error: \ n Source:" + e. Source + "\ n Message:" + e. Message;
Console. WriteLine (msg );
}
}
}

Above
Ad. no = Convert. ToUInt32 (Console. ReadLine ());
To convert the string type returned by ReadLine () to the uint type, the conversion is normal when the input is a number; but if the input is null or other characters, an exception is thrown:
System. FormatException not processed
Message = "the format of the input string is incorrect. "
Source = "mscorlib"
StackTrace:
In System. Number. StringToNumber (String str, NumberStyles options, NumberBuffer & number, NumberFormatInfo info, Boolean parseDecimal)
In System. Number. ParseUInt32 (String value, NumberStyles options, NumberFormatInfo numfmt)
In Welcome. Main ()
The Convert. ToUInt32 (String) function converts the specified String representation of a number to an equivalent 32-bit unsigned integer. The prototype is as follows:
[CLSCompliantAttribute (false)] public static uint ToUInt32 (string value)
It is equivalent to a 32-bit unsigned integer of the value. -Or-if the value is a null reference (Nothing in Visual Basic), it is zero.
The returned value is the result of calling the Int32.Parse Method for value.
If the value is not composed of an optional Symbol followed by a numerical sequence (0 to 9), a FormatException is thrown.
OverflowException is thrown when value indicates a number smaller than MinValue or greater than MaxValue.

FormatException is described as follows:
FormatException is thrown when the format of parameters in a method call does not conform to the format of the corresponding parameter type. For example, if a method specifies a String parameter, which consists of two digits with an embedded period, passing a String parameter containing only two digits to the method will cause FormatException.
FormatException uses the HRESULT COR_E_FORMAT with a value of 0x80131537.

Apparently, in the above case, a formattoo toin exception is thrown because the input character is not a numerical sequence consisting of 0-9. Here, you need to prompt that the user input is invalid, enter a number 0-9 again, instead of terminating the program. Therefore, the exception should be handled as follows:
 
While (true)
{
Try
{
Console. WriteLine ("Enter your age :");
M_1.age = Convert. ToUInt32 (Console. ReadLine ());
Break;
}
Catch (FormatException e)
{
Console. WriteLine ("enter a 0-9 number! ");
}
}

The loop jumps out only when the input is correct. Otherwise, the user is prompted to enter the loop continuously when an exception occurs.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.