Avatar of BlakeMcKenna
BlakeMcKenna
Flag for United States of America asked on

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

Visual Basic.NET

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
Mlanda T

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.
BlakeMcKenna

ASKER
Are we talking about the same thing here.

Your referring to "IsNot" and I'm referring to "IsNothing"...
Mlanda T

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"
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
BlakeMcKenna

ASKER
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

kaufmed

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

ASKER
So I should use

If Not e.Value Is Nothing

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
BlakeMcKenna

ASKER
Not sure why this isn't sticking to memory.

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

Open in new window

ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
BlakeMcKenna

ASKER
Thanks Kaufmed...that helped!
kaufmed

I added in code just after you accepted to demonstrate.

Also, I think MlandaT answered your original question above (http:#a40291514).
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck