[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Do these statements have the same effect?

Posted on 2014-08-28
11
Medium Priority
?
100 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

0
Comment
Question by:BlakeMcKenna
  • 5
  • 4
  • 2
11 Comments
 
LVL 31

Expert Comment

by:MlandaT
ID: 40291435
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
 

Author Comment

by:BlakeMcKenna
ID: 40291474
Are we talking about the same thing here.

Your referring to "IsNot" and I'm referring to "IsNothing"...
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 40291514
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BlakeMcKenna
ID: 40291525
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40291537
IsDBNull is completely different than Is( )Nothing. DBNull is an object; Nothing is not.
0
 

Author Comment

by:BlakeMcKenna
ID: 40291544
So I should use

If Not e.Value Is Nothing

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 2000 total points
ID: 40291547
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
 

Author Comment

by:BlakeMcKenna
ID: 40291606
Not sure why this isn't sticking to memory.

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

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40291616
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
 

Author Closing Comment

by:BlakeMcKenna
ID: 40291622
Thanks Kaufmed...that helped!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40291627
I added in code just after you accepted to demonstrate.

Also, I think MlandaT answered your original question above (http:#a40291514).
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question