Convert.ToString bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Stacey

    #1

    Convert.ToString bug?

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

    Convert.ToStrin g() 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.ToStrin g(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


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Convert.ToStrin g bug?

    William,
    Convert.ToStrin g(string) is defined to return the object that you pass. If
    you pass null to this function you get null back.

    Convert.ToStrin g(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" <staceyw@mvps.o rg> wrote in message
    news:eNgFyn0YDH A.1096@tk2msftn gp13.phx.gbl...[color=blue]
    > Example line:
    > string temp = Convert.ToStrin g(null);
    >
    > Convert.ToStrin g() 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[/color]
    triming[color=blue]
    > 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.ToStrin g(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
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: Convert.ToStrin g bug?

      Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=blue]
      > Convert.ToStrin g(string) is defined to return the object that you pass. If
      > you pass null to this function you get null back.
      >
      > Convert.ToStrin g(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.[/color]

      Just to demonstrate that:

      using System;

      public class Test
      {
      static void Main()
      {
      string x = Convert.ToStrin g ((string)null);
      Console.WriteLi ne (x==null); // Prints true
      x = Convert.ToStrin g ((object)null);
      Console.WriteLi ne (x==null); // Prints false
      }

      }

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...