473,380 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,380 software developers and data experts.

Diff between convert.ToDouble and (Double)

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

Apr 4 '07 #1
3 8699
mr************@gmail.com wrote:
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
Just an intuitive explanation from my own:

Convert is used for complex operations which are rather likely to fail:
e.g. a string is often something different than a integer, real, etc.

Cast is for very similar types. e.g. an integer can nearly always be
converted to a real (except of out of range and such).

For the real idea behind the separation ask someone from MS..
Apr 4 '07 #2
On Apr 4, 6:03 am, mrajanikris...@gmail.com wrote:
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.

Apr 4 '07 #3
mr************@gmail.com wrote:
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
A cast is used when converting between values of the same kind, like
converting an int to double.

The Convert.ToDouble method does different things depending on the data
type of the argument. The Convert.ToDouble(int) just does a cast, while
Convert.ToDouble(string) parses the string, i.e. it does the same thing
as the double.Parse method.

--
Göran Andersson
_____
http://www.guffa.com
Apr 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
10
by: | last post by:
Hey I am calling Convert.ToDouble(someString); But this double can have to ways or representing doubles dependant on the locale, it can be 1.0 or 1,0 Is there a way to make...
3
by: user | last post by:
Hello i have problems with this function, always thru exception for all data, for example: Convert.ToDouble("545.0"). Why ? I want to convert string data in different format into double, i have...
0
by: R. John Reed | last post by:
Hi All, I'm am looking to convert a currency string (e.g. "$1,234.56" to a double value). It appears this will work: double val = Convert.ToDouble(Double.Parse­("$123,456.78901",...
4
by: Daniel Walzenbach | last post by:
Hi, I wonder if somebody could explain me the difference between Double.Parse and Convert.ToDouble. If I'm not mistaken they are implemented differently (I though for a moment they might be the same...
2
by: Jason | last post by:
In VB.NET, when I use System.Convert.ToDouble(string Val) to convert a string variable to double variable, I got something interesting: Dim stringVal As String = "101.01" Dim doubleVal As Double...
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
4
by: Yoavo | last post by:
Hi, I want to convert a string to double. I use the function: "System.Convert.ToDouble". The problem is that if the string contains the character "." the program aborts. What might be the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.