469,621 Members | 1,839 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

validating a null string and an empty string in same IF statement?

VMI
How can I validate a null string or an empty string in the same IF
statement?
If I use this:

if (myString.Trim().Length <= 0 || myString == null)
{
//do stuff
}

I'll get a run-time error because I can't trim (or get the length) of a Null
value. But I don't want to have two IFs (one that validates null and one
that validates an empty string). And I don't want to convert the null value
into a something that can be validated.

Any solution?

Thanks.
Nov 16 '05 #1
5 2204
VMI <vo******@yahoo.com> wrote:
How can I validate a null string or an empty string in the same IF
statement?
If I use this:

if (myString.Trim().Length <= 0 || myString == null)
{
//do stuff
}

I'll get a run-time error because I can't trim (or get the length) of a Null
value. But I don't want to have two IFs (one that validates null and one
that validates an empty string). And I don't want to convert the null value
into a something that can be validated.


Do it the other way round:

if (myString==null || myString.Trim().Length==0)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
VMI wrote:
How can I validate a null string or an empty string in the same IF
statement?
If I use this:

if (myString.Trim().Length <= 0 || myString == null)
{
//do stuff
}

I'll get a run-time error because I can't trim (or get the length) of a Null
value. But I don't want to have two IFs (one that validates null and one
that validates an empty string). And I don't want to convert the null value
into a something that can be validated.

Any solution?


Put the null check first. If the string is null, the first check will
validate to true. Since you have an or condition, the second check for
length won't be executed if the first check validates to true.
--
Tom Porterfield
Nov 16 '05 #3
VMI
Thanks. It worked.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
VMI <vo******@yahoo.com> wrote:
How can I validate a null string or an empty string in the same IF
statement?
If I use this:

if (myString.Trim().Length <= 0 || myString == null)
{
//do stuff
}

I'll get a run-time error because I can't trim (or get the length) of a
Null
value. But I don't want to have two IFs (one that validates null and one
that validates an empty string). And I don't want to convert the null
value
into a something that can be validated.


Do it the other way round:

if (myString==null || myString.Trim().Length==0)

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

Nov 16 '05 #4
It should also be noted that in .NET 2.0, the String class has a new
static method, IsNullOrEmpty, which can be used in this situation.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
VMI <vo******@yahoo.com> wrote:
How can I validate a null string or an empty string in the same IF
statement?
If I use this:

if (myString.Trim().Length <= 0 || myString == null)
{
//do stuff
}

I'll get a run-time error because I can't trim (or get the length) of a
Null
value. But I don't want to have two IFs (one that validates null and one
that validates an empty string). And I don't want to convert the null
value
into a something that can be validated.


Do it the other way round:

if (myString==null || myString.Trim().Length==0)

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

Nov 16 '05 #5
You may also want to do something like so as if Trim is what your after, you
only need to do it once using this method, then test it. Otherwise you
probably need to trim again after the test.
if ( s == null || ((string)(s = s.Trim())).Length == 0 )
{
Console.WriteLine("Null or empty.");
return;
}

--
William Stacey, MVP

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
VMI <vo******@yahoo.com> wrote:
How can I validate a null string or an empty string in the same IF
statement?
If I use this:

if (myString.Trim().Length <= 0 || myString == null)
{
//do stuff
}

I'll get a run-time error because I can't trim (or get the length) of a Null value. But I don't want to have two IFs (one that validates null and one that validates an empty string). And I don't want to convert the null value into a something that can be validated.


Do it the other way round:

if (myString==null || myString.Trim().Length==0)

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


Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by iStrain | last post: by
7 posts views Thread by BlueDragon | last post: by
6 posts views Thread by TJS | last post: by
9 posts views Thread by D. Shane Fowlkes | last post: by
5 posts views Thread by tshad | last post: by
27 posts views Thread by Terry | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.