473,386 Members | 1,705 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,386 software developers and data experts.

Convert.ToString bug?

Example line:
string temp = Convert.ToString(null);

Convert.ToString() says it will return empty string if null is passed as
parm. This returns a null. Is this oversight in the Convert method?

What is the way you guys are checking for nulls on string parms and triming
if they pass something other then null. I was thinking this would do in
most cases, but does not because of "error above"

private void testnull(string test)
{
test = Convert.ToString(test).Trim(); //one line would be nice.
if ( test == null ) //would rather check for "test" equals "" here, then
have to test for null first and then do a trim().
MessageBox.Show(this, "test2 is null.");
else
MessageBox.Show(this, "test2 is not null.");
}

--wjs
Nov 15 '05 #1
2 18467
William,
Convert.ToString(string) is defined to return the object that you pass. If
you pass null to this function you get null back.

Convert.ToString(object) is defined to return the string representation of
the object you pass. If you pass null to this function you get String.Empty
back.

In your function, you are passing test as a string, so the first function is
going to be called.

Hope this helps
Jay

"William Stacey" <st*****@mvps.org> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
Example line:
string temp = Convert.ToString(null);

Convert.ToString() says it will return empty string if null is passed as
parm. This returns a null. Is this oversight in the Convert method?

What is the way you guys are checking for nulls on string parms and triming if they pass something other then null. I was thinking this would do in
most cases, but does not because of "error above"

private void testnull(string test)
{
test = Convert.ToString(test).Trim(); //one line would be nice.
if ( test == null ) //would rather check for "test" equals "" here, then
have to test for null first and then do a trim().
MessageBox.Show(this, "test2 is null.");
else
MessageBox.Show(this, "test2 is not null.");
}

--wjs

Nov 15 '05 #2
Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote:
Convert.ToString(string) is defined to return the object that you pass. If
you pass null to this function you get null back.

Convert.ToString(object) is defined to return the string representation of
the object you pass. If you pass null to this function you get String.Empty
back.

In your function, you are passing test as a string, so the first function is
going to be called.


Just to demonstrate that:

using System;

public class Test
{
static void Main()
{
string x = Convert.ToString ((string)null);
Console.WriteLine (x==null); // Prints true
x = Convert.ToString ((object)null);
Console.WriteLine (x==null); // Prints false
}

}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3

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

Similar topics

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...
1
by: Mike | last post by:
Hi, I was wondering whether a difference existed between the Convert.ToString() method and the method ToString() that is sometimes available in some classes. Is one method preferred over another?...
2
by: Dave | last post by:
Hi, I'm trying to convert a byte array to string --This works... System.BitConverter.ToString(bytes) "EB-55-79-20-18-B2-76-4D-85-0A-93-6B-97-33-31-B8" --This doesn't, but returns...
4
by: simon | last post by:
I have datetime variable: DateTime datum; I set it and when I try to put it into a label: labelDate.Text =datum.ToShortDateString; I get an error:
3
by: Thubaiti | last post by:
Hi, I have this code in my ASP.NET and I want to convert it to C# (code behind) <asp:Repeater id="subCategoryRepeater" runat="server"> <ItemTemplate> <ul> <li> <asp:HyperLink...
3
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
4
by: Jonathan Wood | last post by:
Does anyone know why the toBase argument in Convert.ToString() is limited to 2, 8, 10, or 16? It takes virtually the same code to support all base values from 2 to 36. And can anyone tell me if...
4
by: tshad | last post by:
I am trying to convert a string character to an int where the string is all numbers. I tried: int test; string stemp = "5"; test = Convert.ToInt32(stemp); But test is equal to 53.
2
by: DaveRook | last post by:
I have a question about the Convert.ToString vs ToString. Please find two lines of code: string dt = System.DateTime.Today.ToString("yy/dd/mm"); string date =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.