Loading ...

Boxing and Unboxing

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Boxing and Unboxing

Boxing and Unboxing

Posts under the topic: Boxing and Unboxing

Posted: 10/18/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

 

mohit said:

user="mohit kumar"]

Third -

 

string abc = "Test";
int xyz = (int) abc;//Error - cannot cast string to int.

 

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.


Posted: 10/19/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490

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??


Posted: 10/19/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424

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??

Nothing.. he said that code is working fine.. Laughing


Posted: 10/19/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Thank you Vinz.

can you plz explain second -

 

char abc = 'a';
int xyz = abc;
Response.Write(xyz);

 


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.

 

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??


Posted: 10/19/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

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");
        }



Posted: 10/19/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

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".

 


Posted: 10/19/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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.Smile


Posted: 10/19/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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???


Posted: 10/19/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

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".


Posted: 10/19/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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".

Thank you Vinz.


Posted: 10/19/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Is anyone have different thoughts??? 


Page 1 of 1 (12 items)