views:

448

answers:

6

I always seem to see if a string (querystring value usually) has a value but first I have to check that it is not nothing first so I end up with 2 if then statements - am I missing somethign here - there has to be a better way to do this:

If Not String.IsNullOrEmpty(myString) Then
  If CBool(myString) Then
   //code
   End If
End If
+1  A: 

The boolean "and" operator in your language of choice? Conditionals generally short-circuit so if the first one fails, the second won't run/error.

you want to check that vb.net short-circuits... vbscript didn't. (Huge annoyance)
alumb
A: 

The problem with that is I get an Invalid Cast Exception because myString is nothing when the Cbool is applied, here is the error:

Conversion from string "" to type 'Boolean' is not valid.

Slee
Check my comment on your original post.
Tom Anderson
+3  A: 

In VB.Net, And does not short-circuit, but AndAlso does.

(same for Or and OrElse)

So your code should look something like

If Not String.IsNullOrEmpty(myString) AndAlso CBool(myString) Then 
    ....
End If
devio
I knew I was missing something simple - thank you!
Slee
A: 

FYI this type of code fits perfect as an "extension method". In short this means that you can extend string with a method providing your own code.

Look it up :)

l3dx
But you can't extend a type with static methods (though I think F# allows this). So you can't have String.IsNotNullOrEmptyOrFalse() - but you could have "abcd".IsNotNullOrEmptyOrFalse(). But that's just confusing if you call it a null object.
Mark Brackett
Yes you can - I have written a number of extension methods of the type: String.IsEmail(), String.IsDecimal(), String.IsDate, etc - that's the whole point of Extension Methods: "Extension methods enable you to "add" methods to existing types without creating a new derived type"
Zhaph - Ben Duguid
@Zhaph - the extensions are off an *instance* of the type. Meaning, you'd call it like "abcd".IsEmail(). You can't do an extension like String.IsEmail("abcd"). So, if you have a null string, your code would effectively read null.IsNotNullOrEmptyOrFalse. Which is just weird (though legal).
Mark Brackett
+1  A: 

AndAlso (as mentioned) is the most general purpose answer. But, you could also use the various TryParse methods, which will make code like this:

Dim b as Boolean
If Boolean.TryParse(myString, b) AndAlso b Then
End If

Bonus, it'll save you from the FormatException when someone sends in "blah" in your querystring.

Mark Brackett
downvote because the "AndAlso b" is evaluating if b is true. it should simply read: Dim b as Boolean If Boolean.TryParse(myString, b) Then End If
Russ Bradberry
@Russ - nope, the AndAlso b is (mostly) equivalent to the original If CBool(myString) line. In this scenario, you need both.
Mark Brackett
did you run the code? because i did and what i am saying is true. you dont need "AndAlso b"
Russ Bradberry
If myString is "False", then you need AndAlso b, otherwise you'd run the inner block - when the original code would not.
Mark Brackett
A: 

Use the following if using .NET 2.0 or greater: String.IsNullOrEmpty(thestringobject)

Will evaluate to True or False depending. I usually create another function in a Utility Class that does this check and also checks the trim/length so I know if the string object is null, empty, or a bunch of blanks.