Posted: 10/18/2010
Hello Experts,
Boxing - convert value type to reference type
Unboxing - convert reference type to value type
Above the definition of boxing and unboxing. I have tested some below code and got unexpected output.
first -
int i = 3; object o=i; //boxing int j = (int)o; // unboxing
Above code is working fine.
Second -
char abc = 'a'; int xyz = abc; Response.Write(xyz);
Output is 97.97 ASCII code of a.can anyone tell me why I am getting 97 and also why I don need to cast char to int???
Third -
string abc = "Test"; int xyz = (int) abc;//Error - cannot cast string to int.
and If I use Convert.toInt32.
string abc = "Test"; int xyz = Convert.ToInt32(abc);//Error - Input string was not in a correct format.
but if I use integer value in string.It is working fine
string abc = "23"; int xyz = Convert.ToInt32(abc); Response.Write(xyz);
Output is 23.Now why is it working?
If anyone know why I am getting these error, Kindly explain.
Thanks in advance.
Posted: 10/19/2010
mohit said: user="mohit kumar"]Third - string abc = "Test"; int xyz = (int) abc;//Error - cannot cast string to int.
user="mohit kumar"]
The error seems to be very logical.. that means you cannot cast a string value to an int type. The int type can only represent integers in the range from negative 2,147,483,648 to positive 2,147,483,647, inclusive.
In your example above "Test" is not a valid integer and so it throws an error.
mohit said: user="mohit kumar"]and If I use Convert.toInt32. string abc = "Test"; int xyz = Convert.ToInt32(abc);//Error - Input string was not in a correct format.
Same as above, the "Test" value is not a valid integer value and you are trying to convert the value of "Test" to int that's why it gives you that error. In order to succeed the convertion then assign a valid string value that contains the number to convert.
mohit said: user="mohit kumar"]first - int i = 3; object o=i; //boxing int j = (int)o; // unboxing
What is the unexpected thing in this??
raghav_khunger said: user="Raghav Khunger"] mohit said: user="mohit kumar"]first - int i = 3; object o=i; //boxing int j = (int)o; // unboxing What is the unexpected thing in this??
user="Raghav Khunger"]
Nothing.. he said that code is working fine..
Thank you Vinz.
can you plz explain second -
As you said
Vinz said: user="Vincent Maverick Durano"] The error seems to be very logical.. that means you cannot cast a string value to an int type. The int type can only represent integers in the range from negative 2,147,483,648 to positive 2,147,483,647, inclusive.In your example above "Test" is not a valid integer and so it throws an error. mohit said: user="mohit kumar"]and If I use Convert.toInt32. string abc = "Test"; int xyz = Convert.ToInt32(abc);//Error - Input string was not in a correct format. Same as above, the "Test" value is not a valid integer value and you are trying to convert the value of "Test" to int that's why it gives you that error. In order to succeed the convertion then assign a valid string value that contains the number to convert.
user="Vincent Maverick Durano"]
Vinz that I know what you said.But if I am using textbox value and convert it into in int.That time I wouldn't know about data type of value.So can you please tell me what should I do in textbox situation??
mohit said: user="mohit kumar"]Vinz that I know what you said.But if I am using textbox value and convert it into in int.That time I wouldn't know about data type of value.So can you please tell me what should I do in textbox situation??
user="mohit kumar"]Vinz that I know what you said.But if I am using textbox value and convert it into in int.That time I wouldn't know about data type of value.So can you please tell me what should I do in textbox situation??
Use int.TryParse method to avoid format exeptions when converting string value to int.
string sVal = TextBox1.Text; int result =0; if (int.TryParse(sVal, out result)) { Response.Write("String is a valid Integer value"); //Do something } else { Response.Write("String is NOT a valid Integer value"); }
mohit said: user="mohit kumar"]can you plz explain second - char abc = 'a'; int xyz = abc; Response.Write(xyz);
Char is an integer type, so you really don't need the Convert.ToInt32() method. You are getting the value of 97 because that's the ASCII value of the char "a".
Vinz said: user="Vincent Maverick Durano"] mohit said: user="mohit kumar"]Vinz that I know what you said.But if I am using textbox value and convert it into in int.That time I wouldn't know about data type of value.So can you please tell me what should I do in textbox situation?? Use int.TryParse method to avoid format exeptions when converting string value to int. string sVal = TextBox1.Text; int result =0; if (int.TryParse(sVal, out result)) { Response.Write("String is a valid Integer value"); //Do something } else { Response.Write("String is NOT a valid Integer value"); }
Thank you Vinz.Now I will use Int.TryParse.
Vinz said: user="Vincent Maverick Durano"] mohit said: user="mohit kumar"]can you plz explain second - char abc = 'a'; int xyz = abc; Response.Write(xyz); Char is an integer type, so you really don't need the Convert.ToInt32() method. You are getting the value of 97 because that's the ASCII value of the char "a".
Yes Vinz you are right chat and int both are value type too. May I know why I am getting ASCII value of a???
mohit said: user="mohit kumar"]May I know why I am getting ASCII value of a???
user="mohit kumar"]May I know why I am getting ASCII value of a???
because the variable abc that contains the value of "a" is a char datatype and you convert the char value to int that's why it gives you the numerical value for the char "a". Simple!
To make it more clearer.. a char datatype holds a single Unicode/ASCII character. The Unicode character represented by the number ninety seven is the small letter "a".
Vinz said: user="Vincent Maverick Durano"] mohit said: user="mohit kumar"]May I know why I am getting ASCII value of a??? because the variable abc that contains the value of "a" is a char datatype and you convert the char value to int that's why it gives you the numerical value for the char "a". Simple!To make it more clearer.. a char datatype holds a single Unicode/ASCII character. The Unicode character represented by the number ninety seven is the small letter "a".
Is anyone have different thoughts???