Check a string for nothing and then for a specific value in 1 line?
I seem to always see if the string (querystring value) has a value, but first I have to check that this is not the first time, so I end up with 2, if it does, I don't see anything like it here. this is the best way to do it:
If Not String.IsNullOrEmpty(myString) Then
If CBool(myString) Then
//code
End If
End If
+1
source to share
5 answers
AndAlso (as mentioned) is the most versatile answer. But you can also use various TryParse methods that will do the code like this:
Dim b as Boolean
If Boolean.TryParse(myString, b) AndAlso b Then
End If
Bonus, it will save you a FormatException when someone sends "blah" to your request.
+1
source to share