Hi Friends,
I am accepting a number from the user entered in a textbox.
I want to assign to a variable in my code and assignt this to that
variable.
double num1 = (double)txtNum1.text;
this produced an error
double num1 = convert.ToDouble(txtNum1.Text);
This succeeded.
What is the difference b/w these two statements.
Thanks in advance
Again, not technical, but my own interpretation:
Convert is a series of functions specifically designed to convert
various primitive datatypes between each other. It performs input
parsing and anlaysis to determine the desired output from the input
etc. The original data item in maintained, and a new object is created
that matches the desired type.
The cast operation on the other hand is not meant to convert & create
a new object. It's purpose to deal with the issues that occur when
using inheritance to store objects with pointers to them. If you are
unfamiliar with these concepts, perform a google search on inheritance
and pollymorphism.
The best example I can think of to demonstrate the use of the cast
operation vs. convert is storing Session values (using the InProc
method). Say a user on your web page enters a numerical value in a
TextBox on your webpage, then clicks the submit button. You want to
take this value, and store it into a Session variable for later use on
other webpages. You're code would look like the following:
int mynum = Convert.ToInt32 (TextBox1.Text);
Page.Session["mynum"] = mynum;
Here we have used the Convert function to take the string value that
is return by TextBox1.Text, and get a new object that is an int, and
we stored that in the mynum variable. That variable was then stored
into the Session collection AS AN INT32.
Using Session though, we can store any object type we want. Ints,
strings, doubles, or even complex objects like DataSets,
SqlConnections, etc. They are all stored directly into the Session
collection as their "native" object types without any conversion.
Thus, on a later page we should be able to retrieve our Session item
again without using any conversion, like the following:
int mynum = Page.Session["mynum"];
If you were to try and compile this though, the compilation would fail
due to a type conversion error. Because Session can be used to store
any datatype, it is coded such that the datatype the it returns is
reported as the back Object type. This is alright though, as every
object type in C# is derived from (inherited) the base Object type.
What we need to do is tell the compiler explicitly that the actual
object that is being returned by the Session variable is a type
inherited from Object. This is where you use the cast operation:
int mynum = (int) Page.Session["mynum"];
Here, we've told the compiler that even though Session reports that
it's returning an object, we know that it will return an integer, and
the compiler will accept our overide and compile the page. When the
page load and we try to retrieve the session object, all that will
happen is that the run-time engine will perform a quick check on the
Session object to varify that it is actually an int (instead of some
other datatype), and store that int in out mynum variable.
Long story short: You want to use Convert when you are actually
changing the data from one kind to another. You want to use casting
when you know that the type that the compiler thinks it is is wrong
and you want to overide it, not performing any type of conversion.
I hope that clears up the issue a little for you.