replace null with ""

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Huang

    replace null with ""

    Hi,

    I would like to have a public string which will return a "" if the parameter
    is a Null string, would someone tell me what's wrong with my code?

    public string NoNull(string strNoNull)
    {
    if (strNoNull==nul l)
    {
    return "";
    }
    else
    {
    strNoNull=strNo Null;
    }

    Thanks for help.

    Jason


  • Tim Haughton

    #2
    Re: replace null with ""

    Hi Jason, try this...

    public string NoNull( string arg )
    {
    if ( arg == null )
    return "";
    else
    return arg;
    }

    Things I can see wrong with it - not all code paths return a value. And this
    :
    [color=blue]
    > else
    > {
    > strNoNull=strNo Null;
    > }[/color]

    What do you think this is doing?

    --
    Regards,

    Tim Haughton

    Agitek



    "Jason Huang" <JasonHuang8888 @hotmail.com> wrote in message
    news:epcweQgsFH A.1172@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hi,
    >
    > I would like to have a public string which will return a "" if the[/color]
    parameter[color=blue]
    > is a Null string, would someone tell me what's wrong with my code?
    >
    > public string NoNull(string strNoNull)
    > {
    > if (strNoNull==nul l)
    > {
    > return "";
    > }
    > else
    > {
    > strNoNull=strNo Null;
    > }
    >
    > Thanks for help.
    >
    > Jason
    >
    >[/color]


    Comment

    • Oliver Sturm

      #3
      Re: replace null with &quot;&quot;

      Tim Haughton wrote:
      [color=blue]
      >public string NoNull( string arg )
      >{
      > if ( arg == null )
      > return "";
      > else
      > return arg;
      >}[/color]

      If you're on .NET 2, try the new ?? operator to do the same thing:

      return arg ?? "";



      Oliver Sturm
      --
      omnibus ex nihilo ducendis sufficit unum
      Spaces inserted to prevent google email destruction:
      MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
      ICQ 27142619 http://www.sturmnet.org/blog

      Comment

      • Joanna Carter \(TeamB\)

        #4
        Re: replace null with &quot;&quot;

        "Oliver Sturm" <oliver@sturmne t.org> a écrit dans le message de news:
        xn0e6vw459ok8i7 004@msnews.micr osoft.com...
        [color=blue][color=green]
        > >public string NoNull( string arg )
        > >{
        > > if ( arg == null )
        > > return "";
        > > else
        > > return arg;
        > >}[/color]
        >
        > If you're on .NET 2, try the new ?? operator to do the same thing:
        >
        > return arg ?? "";[/color]

        ....or even the good old ?: syntax

        public string NoNull( string arg )
        {
        return arg == null ? "" : arg;
        }

        Joanna

        --
        Joanna Carter
        Consultant Software Engineer


        Comment

        • Jimbo

          #5
          Re: replace null with &quot;&quot;

          Tim Haughton wrote:[color=blue]
          > Hi Jason, try this...
          >
          > public string NoNull( string arg )
          > {
          > if ( arg == null )
          > return "";
          > else
          > return arg;
          > }
          >[/color]

          Why the else?

          if (arg == null)
          return "";
          return arg;

          Still better is Joanna Carters option of ?:

          Comment

          • TaylorMichaelL

            #6
            Re: replace null with &quot;&quot;

            I agree with Joanna's code. I do it for almost all string properties because
            as the guidelines state a string should really never be null because this
            throws people off. Unless a null string has a different meaning than an
            empty string it pays to be nice. My only recommendation would be to switch
            the boolean operator because you assume that normally it is not null so you
            save a jump instruction (might actually be optimized by the compiler). So...

            public string NoNull ( string arg )
            {
            return arg != null ? arg : "";
            }

            Michael Taylor - 9/5/05

            "Jimbo" wrote:
            [color=blue]
            > Tim Haughton wrote:[color=green]
            > > Hi Jason, try this...
            > >
            > > public string NoNull( string arg )
            > > {
            > > if ( arg == null )
            > > return "";
            > > else
            > > return arg;
            > > }
            > >[/color]
            >
            > Why the else?
            >
            > if (arg == null)
            > return "";
            > return arg;
            >
            > Still better is Joanna Carters option of ?:
            >[/color]

            Comment

            • Javaman59

              #7
              Re: replace null with &quot;&quot;

              Hi, I was just browsing through this thread and noticed this comment...
              [color=blue]
              > My only recommendation would be to switch
              > the boolean operator because you assume that normally it is not null so you
              > save a jump instruction (might actually be optimized by the compiler).[/color]

              Over the years I've often wondered about the best way to handle if.. else..
              for error conditions - specifically whether to test for the erroneous
              condition, or whether to test for the correct condition. The conclusion I've
              reached is one which I can apply to every conditional statement - prefer the
              simplest form of conditions which is correct in the circumstances. For
              instance, prefer == to !=, prefer > to <=, etc. This cut's down on double
              negatives, which I think make code more difficult to read. eg. "if a !=
              b..else.." - if we think, we can see that the else is the "a == b" case, but
              it's not as readable as "if a == b ... else ..", and given that conditions
              can be nested, we should aim build up a sequence of positive, rather than
              negative, conditions.

              The potential saving of a jump statement, in the normal case, by changing
              the condition, is an interesting issue. Thanks for pointing that out.

              M2CW

              Javaman

              "TaylorMichaelL " wrote:
              [color=blue]
              > I agree with Joanna's code. I do it for almost all string properties because
              > as the guidelines state a string should really never be null because this
              > throws people off. Unless a null string has a different meaning than an
              > empty string it pays to be nice. My only recommendation would be to switch
              > the boolean operator because you assume that normally it is not null so you
              > save a jump instruction (might actually be optimized by the compiler). So...
              >
              > public string NoNull ( string arg )
              > {
              > return arg != null ? arg : "";
              > }
              >
              > Michael Taylor - 9/5/05
              >
              > "Jimbo" wrote:
              >[color=green]
              > > Tim Haughton wrote:[color=darkred]
              > > > Hi Jason, try this...
              > > >
              > > > public string NoNull( string arg )
              > > > {
              > > > if ( arg == null )
              > > > return "";
              > > > else
              > > > return arg;
              > > > }
              > > >[/color]
              > >
              > > Why the else?
              > >
              > > if (arg == null)
              > > return "";
              > > return arg;
              > >
              > > Still better is Joanna Carters option of ?:
              > >[/color][/color]

              Comment

              Working...