Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 21st, 2006, 8:37 AM   #11
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3 Master is on a distinguished road
I think he think he is using VB.NET because it automatically does the conversion
Master is offline   Reply With Quote
Old Nov 21st, 2006, 12:14 PM   #12
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5 lectricpharaoh will become famous soon enough
I prefer using the Parse() or TryParse() methods instead- the former is more appropriate if you know the number is okay to convert, while the latter is better for any 'un-vetted' input (particularly user input).

Parse() returns the converted value if it was successful, or otherwise throws an exception. TryParse() returns a bool that is true on success, and fills in the value via an out parameter. Example:
double int value;
bool success;

value = double.Parse("123.45");  // this line will be okay
value = double.Parse("abc.de");  // throws exception
success = double.TryParse("123.45", out value);  // returns true, value is filled in
success = double.TryParse("abc.de", out value);  // returns false, value is unchanged
[edit] I forgot to mention that all the basic types (char, int, etc) have this method. It's actually a method of the 'boxed' classes like Double as opposed to double, but the .NET runtime will handle the boxing/unboxing of data, so you don't need to worry about it. So, in the future, if you ever need to convert a string to a number, these two methods are your best friends. [/edit]
[edit2] Well, I didn't read the thread all the way through, and failed to notice the problem was actually converting the other way around. So yeah, ToString() as several people mentioned works for that (and will work on every single type, although you might not always get the text you expect). [/edit2]
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Problem Associated with Vector Source code buggytoast Java 3 Apr 2nd, 2006 5:41 AM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 5:45 PM
help out beginner with some errors (solved) monka Java 8 Mar 3rd, 2005 8:50 PM
Y for yes and N for no problem sobank C++ 4 Mar 1st, 2005 10:33 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC