PC Review


Reply
Thread Tools Rate Thread

Converting number to String - Different Approaches

 
 
mac
Guest
Posts: n/a
 
      20th Jul 2006
Assume you have a textbox named "displayValue"

What is the difference (if any) between these two approaches:

1.)
float var;
var = 0.42F;
displayValue.Text = var.ToString

2.)
float var;
var = 0.42F;
displayValue.Text = Convert.ToString(var);


Is one way generally preferred over the other?


Thanks,

Mac


 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWljaGFlbCBOZW10c2V2?=
Guest
Posts: n/a
 
      20th Jul 2006
These both type casts are almost identical.

Convert.ToString just call your value.ToString(CultureInfo.CurrentCulture);
For some types it miss CultureInfo.CurrentCulture

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche




"mac" wrote:

> Assume you have a textbox named "displayValue"
>
> What is the difference (if any) between these two approaches:
>
> 1.)
> float var;
> var = 0.42F;
> displayValue.Text = var.ToString
>
> 2.)
> float var;
> var = 0.42F;
> displayValue.Text = Convert.ToString(var);
>
>
> Is one way generally preferred over the other?
>
>
> Thanks,
>
> Mac
>
>
>

 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      20th Jul 2006
"mac" <(E-Mail Removed)> wrote:

> Assume you have a textbox named "displayValue"
>
> What is the difference (if any) between these two approaches:
>
> 1.)
> float var;
> var = 0.42F;
> displayValue.Text = var.ToString


ToString() can accept IFormatProvider and a string format, making it
more flexible. If you have a custom value type and it overrides
ToString(), then calling it directly on the instance won't cause boxing.

> 2.)
> float var;
> var = 0.42F;
> displayValue.Text = Convert.ToString(var);


This will call var.ToString, and may cause boxing if your value type is
not special-cased by Convert (float is, of course).

> Is one way generally preferred over the other?


I personally never use the Convert class except for Base64 conversions.
For most "display value" kind of conversions, I typically use
string.Format().

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting string to number kenppy Microsoft Excel New Users 6 5th Mar 2010 07:18 PM
Converting binary string to different whole number OMS Microsoft Access 13 25th Jan 2008 08:20 PM
converting number string to number with decimal =?Utf-8?B?cm9ydGl6?= Microsoft Excel Worksheet Functions 2 15th Sep 2005 08:34 PM
converting number to string Microsoft Excel Programming 5 23rd Jan 2004 07:38 PM
Converting a number to a string Joakim Microsoft Excel Worksheet Functions 13 11th Aug 2003 09:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:55 AM.