Posts VB.NET Whidbey Release - The IsNot operator.
Post
Cancel

VB.NET Whidbey Release - The IsNot operator.

Paul Vick explains that there will be a new IsNot operator coming in VB.NET.  Although I don't see a problem with this, I'm not sure having it will make any difference in my development life.  Some of the examples I've seen that point out this new operator are using the following example:

If Not o Is Nothing Then

If the above line was the reason for adding IsNot, I don't think it would be enough to justify the new operator.  The reason for this is that you could write the same thing as:

If IsNothing(o) Then

Hmmm, seems a lot smaller and clearer than either the first example or the following using the new operator.

If o IsNot Nothing Then

Seeing this example thrown around, I wondered why do we need a third way to accomplish the same task?

Well, the new operator actually does a lot more.  First, take the Is operator.  It checkes to see if two variables are the same, not the values, but the actual variables themselves.  Do they share the same reference?  The IsNot does a similar task and checks to see that two variables are not sharing the same reference.  OK, IsNothing() just checks that a variable does not share a reference to Nothing... as does all of the previous examples.  However, what if you want to check the following?

Dim a As New Foo
Dim b As New Bar

If a IsNot b Then

or

If Not a Is b Then

Either way, the two (at least to me) appear to be just as simple to write and read.  Maybe I've just gotten used to using the If Not... format.  However, when translating the following code from C#:

if (a != b)

Where a and b are reference types, the IsNot operator makes for an easier transition between the two languages.

I see the IsNot operator as something that is not necessarily needed, however, it doesn't really hurt anything and if it makes even one developers life easier (while not hurting the rest of the VB.NET community); then I'm one happy camper ;-)

Also, as an added bonus, Paul discusses the history/rational as to why there is the Is operator and why it's not mixed together with the = operator (like some other languages).  I accepted that this was the case with VB.NET and actually kind of hated the fact that I could just use the freakin' equal sign to check if two things were equal (reference types and value types, there just variables right?).  After hearing the rational behind the VB.NET language design decision and the move into Whidbey and operator overloading, having the Is operator is probably going to become my new best friend :-D

This post is licensed under CC BY 4.0 by the author.