Empty strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Capel

    Empty strings

    Is there any way to express this more concisely?

    if (someString == null || someString == "")
    throw new ArgumentExcepti on("String must have something in it."); // or
    something

    What would be perfect would be someString.IsEm pty(), but MS didn't see fit
    to include that particular one. Maybe there's something just as good. ??

    Chris


  • Bruno Jouhier [MVP]

    #2
    Re: Empty strings

    > What would be perfect would be someString.IsEm pty(), but MS didn't see fit[color=blue]
    > to include that particular one. Maybe there's something just as good. ??[/color]

    It would have be a static method, something like String.IsEmptyO rNull(string
    str)
    because someString.IsEm pty() will throw a null reference exception if
    someString is null.

    As the MS guys did not provide it, you will have do it (we did). You just
    have to write some kind of StringHelper class with this static method in it.

    Bruno.


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Empty strings

      Chris,

      You should probably change the ArgumentExcepti on to an
      ArgumentNullExc eption, just to be more concise. Otherwise, I would do as
      Bruno recommends, and create your own static method.

      --
      - Nicholas Paldino [.NET/C# MVP]
      - nicholas.paldin o@exisconsultin g.com

      "Chris Capel" <chris@nowhere. com> wrote in message
      news:OJoJc3eVDH A.1704@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Is there any way to express this more concisely?
      >
      > if (someString == null || someString == "")
      > throw new ArgumentExcepti on("String must have something in it."); //[/color]
      or[color=blue]
      > something
      >
      > What would be perfect would be someString.IsEm pty(), but MS didn't see fit
      > to include that particular one. Maybe there's something just as good. ??
      >
      > Chris
      >
      >[/color]


      Comment

      • Val Savvateev

        #4
        Re: Empty strings

        He's also checking if string is zero-length - ArgumentNullExc eption does not
        quite fit in that case.

        ..
        "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
        in message news:O6LP9KfVDH A.2352@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Chris,
        >
        > You should probably change the ArgumentExcepti on to an
        > ArgumentNullExc eption, just to be more concise. Otherwise, I would do as
        > Bruno recommends, and create your own static method.
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - nicholas.paldin o@exisconsultin g.com
        >
        > "Chris Capel" <chris@nowhere. com> wrote in message
        > news:OJoJc3eVDH A.1704@TK2MSFTN GP11.phx.gbl...[color=green]
        > > Is there any way to express this more concisely?
        > >
        > > if (someString == null || someString == "")
        > > throw new ArgumentExcepti on("String must have something in it."); //[/color]
        > or[color=green]
        > > something
        > >
        > > What would be perfect would be someString.IsEm pty(), but MS didn't see[/color][/color]
        fit[color=blue][color=green]
        > > to include that particular one. Maybe there's something just as good.[/color][/color]
        ??[color=blue][color=green]
        > >
        > > Chris
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Gang Peng[MSFT]

          #5
          Re: Empty strings

          Just FYI, if you try Whidbey Alpha, you will see String.IsNullOr Empty is
          added in String class.

          Gang Peng
          [MS]

          Comment

          Working...