473,769 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Diff between convert.ToDoubl e 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.ToDoubl e(txtNum1.Text) ;

This succeeded.

What is the difference b/w these two statements.

Thanks in advance

Apr 4 '07 #1
3 8748
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.ToDoubl e(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.ToDoubl e(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.ToDoubl e(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.ToDoubl e method does different things depending on the data
type of the argument. The Convert.ToDoubl e(int) just does a cast, while
Convert.ToDoubl e(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
47071
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
4
9768
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 numeric value according to an arbitrary regular expression.
10
19666
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 Convert.ToDouble(..) accept both comma and period notations for representing numbers?
3
688
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 formats like: 37.439351 2.00000e+02 How can i do it most easily ?
0
8430
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", System.Globalization.NumberSty­les.Currency));
4
11106
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 like cint(anInt) and cType(anInt, System.Int32) but I checked with ildasm) - if I didn't made a mistake. So when to use which syntax? Is there any performance penalty when using the one over the other or does anybody knows any differences? '...
2
6632
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 doubleVal = System.Convert.ToDouble(stringVal) Msgbox (doubleVal) will return 101.01 but...... Dim stringVal As String = "101.00"
4
4523
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 it seems this convert is determined by the local settings and comma is indeed used as decimal separator. Is there another way to convert a dotted value to a double variable? Like 1234.5 and not 1234,5
4
2568
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 problem ? Yoav.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.