Checking for IsNullOrEmpty or is Nothing

In VB .net is it better is it better to check a string for IsNullOrEmpty rather than checking for a Nothing value? Look at the two different code blocks below . Which is better, 1 or 2?

(1)
If Not strMyString Is Nothing Then
End If

(2)
If Not String.IsNullOrEmpty(strMyString) Then
End If
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
It really depends what you want to check I suppose.

I think, (1) would  check if the string hadn't been set and (2) would check if it's an empty string, "" or Null.

So (2) is probably the most appropriate if you want to check the value of the string.
0
ste5anSenior DeveloperCommented:
*cough* not better. Different

Test 1 only tests whether the string is Null (Nothing), but not whether it is empty.
Test 2 tests for non empty strings. This implies it is also not null.

Module Module1
    Sub Main()
        Dim Test = ""
        If Not Test Is Nothing Then
            Console.WriteLine("Not Is Nothing")
        Else
            Console.WriteLine("hmm?")
        End If

        If Not String.IsNullOrEmpty(Test) Then
            Console.WriteLine("Not IsNullOrEmpty")
        Else
            Console.WriteLine("another hmm?")
        End If

        Console.WriteLine("Done.")
        Console.ReadLine()
    End Sub
End Module

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.