Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner's Question on Conversion

Status
Not open for further replies.

sandlen

Programmer
Feb 11, 2003
2
US
Learning C# through a book, and came upon these examples the author gave to convert a string value into an integer. These are:

myInt = Convert.ToInt32("123");

myString = Convert.ToString(123);

The problem is, the author didn't explain why & when I would choose to use one over the other and vice versa.

Also, the author states in another example: "Because you sometimes grab a numeric value from the console, you often simply put the Console.ReadLine() method inside the ToInt() parentheses, like this:"
myInt = Convert.ToInt32(Console.ReadLine());

Again, the author doesn't explain why or when you would do this. If I'm understanding this one (please correct me if I'm wrong) I'm reading in the user input, converting the string to an integer value type and then assigning that value to myInt?

If someone wouldn't mind explaining these to me and perhaps using these examples in a bit of code so that I could see the flow, I would greatly appreciate it. I appreciate your time and help.
 
>>> myInt = Convert.ToInt32(Console.ReadLine());

So, this takes user input from the console, which always comes in as a String. But the problem is, how to you manipulate a String if the input is supposed to be numerical. For example, you cannot perform arithmetic functions on a String. So if you wished to read some numbers in from the console, then times them by 2, then output the result, you need to convert the String to an integer in order to perform the arithmetic function.

Ben
 
sandlen -
Like Sedj pointed out, C# is a strongly-typed language. Meaning that if you have an object in memory, you must always know what data type it is -- whether it's a string, an integer, or a user-defined type (a class you've written). So when you need to convert from one datatype to another, you usually need to go through some sort of conversion function.

One point of confusion for people getting started is the difference between the C# name for a datatype, and the .NET framework equivalent. It's possible to have a variable declared as an "int" and another one declared as "System.Int32". It turns out that the two are identical, because the C# "int" just maps to a "System.Int32" in the runtime library anyway. Which is better to use? From a code efficiency standpoint, it doesn't matter. From a development standards standpoint - you need to pick a style and consistently use it.

You can read more about System.Int32 at:

and the C# "int" at:

Note this bit from the C# reference:
The char type represents unsigned 16-bit integers with values between 0 and 65535. The set of possible values for the char type corresponds to the Unicode character set. Although char has the same representation as ushort, not all operations permitted on one type are permitted on the other.

What this is saying, that while a character takes up the same amount of memory (2 bytes) as an ushort, you can't treat them the same, as you can do math operations on a ushort, but not on a char (it wouldn't make sense to multiply two characters against each other).

Hope this helps.
Chip H.
 
More typically, you read input as a string and then separately try to convert it to a number. This allows you to handle any errors the user made and, for example, ask again for the input.

So, for example, say you have a form that asks for someone's Age as part of form in a text box. A text box's value is a string of whatever the user enters. You can then convert this to a number with appropriate error checking.

Users make mistakes. If someone entered "12 years" or "12.0." as their age, the attempt to convert from a string to an integer will throw an error and you have the opportunity to react programmatically to the problem.

d-

 
here is a easyer way:

myInt = int.Parse("123")
myString = "123.ToString()

these ways are much easyer than using the Convert class, but is up to you what do want to use.

the example that ypu posted works this way:
Console.ReadLine() - is a function that return whatever you typed into a string, meanwhile the Convert.ToInt32(string myString) has a string parameter, so instead of using 2 lines of code u can make just one. The example u gaved is similar to this:
myString = Console.ReadLine();
myInt = Convert.ToInt32(myString);

is just a way to lower the code line number.
 
in the second example loose the " character, i typed wrong :) the correct way is:
myString = 123.ToString()
 
Thank you all for your time and your replies. I have a very good understanding now due to all of your perspectives.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top