We help IT Professionals succeed at work.

Do these statements have the same effect?

BlakeMcKenna
BlakeMcKenna asked
on
112 Views
Last Modified: 2014-08-28
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

Comment
Watch Question

CERTIFIED EXPERT

Commented:
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.

Author

Commented:
Are we talking about the same thing here.

Your referring to "IsNot" and I'm referring to "IsNothing"...
CERTIFIED EXPERT

Commented:
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"

Author

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

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
IsDBNull is completely different than Is( )Nothing. DBNull is an object; Nothing is not.

Author

Commented:
So I should use

If Not e.Value Is Nothing

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Not sure why this isn't sticking to memory.

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

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks Kaufmed...that helped!
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I added in code just after you accepted to demonstrate.

Also, I think MlandaT answered your original question above (http:#a40291514).
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.