Do these statements have the same effect?

Is there a difference between statement 1 and statement 2? Will they have the same outcome?

Statement 1

If Not IsNothing(var1) Then
   Do something
End If

Open in new window


Statement 2

If Not var2 Is Nothing Then
   Do something
End If

Open in new window

BlakeMcKennaAsked:
Who is Participating?
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.

MlandaTCommented:
They are the same.  IsNot is the opposite of the Is operator. The advantage of IsNot is that you can avoid awkward syntax with Not and Is, which can be difficult to read. (http://msdn.microsoft.com/en-us/library/t3bat82c.aspx)

If Not var1 Is Nothing Then

Is a continuation from VB6 where there wasn't an "IsNot," and so this was the only way to determine if a variable was Nothing. They two statements above serve the same purpose. the IsNot makes it easier to read and understand.
0
BlakeMcKennaAuthor Commented:
Are we talking about the same thing here.

Your referring to "IsNot" and I'm referring to "IsNothing"...
0
MlandaTCommented:
Ooops! I'm sorry Blake. You're right there.

That said... IsNothing and "Is Nothing" are intended to achieve the same thing.

However, IsNothing accepts both reference or nullable types and value types (e.g. int, long, double). However, when given a value type - well ... a value type can never be Nothing so it's a wasted check. "Is Nothing" however will only accept reference types and doing a:
Dim value As Integer
If value Is Nothing Then...

Open in new window

will not compile, and the compiler will raise the error "'Is' operator does not accept operands of type 'Integer'. Operands must be reference or nullable types."

IsNothing is also reportedly slower that "Is Nothing" or "IsNot Nothing"
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

BlakeMcKennaAuthor Commented:
Ok,

With that said, what is the best way to check a datagridview cell in the CellPainting Event in order that it doesn't throw an exception before being used? Which syntax is preferred?

Syntax 1
If Not e.Value Is Nothing

Open in new window

or

Syntax 2
If Not IsDBNull(e.Value)

Open in new window

0
käµfm³d 👽Commented:
IsDBNull is completely different than Is( )Nothing. DBNull is an object; Nothing is not.
0
BlakeMcKennaAuthor Commented:
So I should use

If Not e.Value Is Nothing

Open in new window

0
käµfm³d 👽Commented:
Depends on what you are testing for. "Is Nothing" is a test for the absence of value; "IsDBNull" is a test for a value that is DBNull. DBNull is a .NET equivalence for NULL in a database. Nothing is for variables that have either not been assigned or have had their assignment cleared.

e.g.

Dim x As Object = DBNull.Value
Dim y As Object = Nothing

Console.WriteLine(IsDBNull(x))
Console.WriteLine(IsDBNull(y))
Console.WriteLine(x Is Nothing)
Console.WriteLine(y Is Nothing)

Open in new window


Another way of thinking about it:  A variable that "IsDbNull" points to the sole instance of DbNull.Value in memory; a variable that "IsNothing" points to no memory at all.
0
BlakeMcKennaAuthor Commented:
Not sure why this isn't sticking to memory.

Is "Is Nothing" the same as
If var1 = "" 

Open in new window

0
käµfm³d 👽Commented:
No, because empty string ( "" ) is a value.

e.g.

Dim var1 As Object

Console.WriteLine(var1 Is Nothing)
Console.WriteLine("".Equals(var1))  ' We can't use var1 = "" because VB treats this as an assignment
var1 = ""
Console.WriteLine(var1 Is Nothing)
Console.WriteLine("".Equals(var1))  ' We can't use var1 = "" because VB treats this as an assignment
var1 = Nothing
Console.WriteLine(var1 Is Nothing)
Console.WriteLine("".Equals(var1))  ' We can't use var1 = "" because VB treats this as an assignment

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
BlakeMcKennaAuthor Commented:
Thanks Kaufmed...that helped!
0
käµfm³d 👽Commented:
I added in code just after you accepted to demonstrate.

Also, I think MlandaT answered your original question above (http:#a40291514).
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.