views:

788

answers:

7

I'm having the exciting task of finding out about VB.NET's <> and Not operators. Not - I'm assuming by my small use of it - is the functional equivalent of ! in languages such as C# and <> being equivalent of !=.

In VB.NET a common problem is doing Boolean expressions against objects that don't have a reference, it appears. So if we do

If Request.QueryString("MyQueryString") <> Nothing Then

This will actually fail if the query string doesn't exist. Why, I don't know. The way that it's done by older coders is as follows:

If Not Request.QueryString("MyQueryString") Is Nothing Then

And this tends to work. To me they're functionally equivalent though operators tend to do different comparisons dependent on certain factors such as operator precedence, why it doesn't work in this case however, I do not know, and neither have I found any relevant material.

I ask this as I'm having to write standards documentation and we're determining the use of either the Not or <>. Any ideas on which way around it should be, or you should do it?

+7  A: 

I have always used the following:

If Request.QueryString("MyQueryString") IsNot Nothing Then

But only because syntactically it reads better.

When testing for a valid QueryString entry I also use the following:

If Not String.IsNullOrEmpty(Request.QueryString("MyQueryString")) Then

These are just the methods I have always used so I could not justify their useage other than they make the most sense to me when reading back code.

Charlie
So just for clarification, what's the difference between these two? IsNot Nothing is checking for a null reference I assume, but the latter example also checks for emptiness or a null `value` as opposed to reference?
Kezzer
Excellent answer, @Charles. Almost identical to what I would've written. +1
Cerebrus
Kezzer - The top one is merely checking for an unpopulated variable e.g. null/nothiing. It would not detect an empty string, I guess the second one is a more complete statement and does more work for you.
Charlie
+4  A: 

Is is not the same as = -- Is compares the references, whilst = will compare the values.

If you're using v2 of the .Net Framework (or later), there is the IsNot operator which will do the right thing, and read more naturally.

Rowland Shaw
A: 

Just use which sounds better. I'd use the first approach, though, because it seems to have fewer operations.

luiscubal
+1  A: 

I think your question boils down to "the difference between (Is and =) and also (IsNot and <>)".

The answer in both cases is the same :

= and <> are implicitly defined for value types and you can explicitly define them for your types.

Is and IsNot are designed for comparisons between reference types to check if the two references refer to the same object.

In your example, you are comparing a string object to Nothing (Null) and since the =/<> operators are defined for strings, the first example works. However, it does not work when a Null is encountered because Strings are reference types and can be Null. The better way (as you guessed) is the latter version using Is/IsNot.

Cerebrus
A: 

Here's the technical answer (expanding on Rowland Shaw's answer).

The Is keyword checks whether the two operands are references to the same object memory, and only returns true if this is the case. I believe it is functionally equivalent to Object.ReferenceEquals. The IsNot keyword is simply shorthand syntax for writing Not ... Is ..., nothing more.

The = (equality) operator compares values and in this case (as in many others) is equivalent to String.Equals. Now, the <> (inequality) operator does not quite have the same analogy as the Is and IsNot keywords, since it can be overriden separately from the = operator depending on the class. I believe the case should always be that it returns the logical inverse of the = operator (and certainly is in the case of String), and just allows for a more efficient comparison to made when testing for inequality rather than equality.

When dealing with strings, unless you actually mean to compare references, always use the = operator (or String.Equals I suppose). In your case, because you are testing for null (Nothing), it seems you need to use the Is or IsNot keyword (the equality operator will fail because it can't compare the values of null objects). Syntactically, the IsNot keyword is a bit nicer here, so go with that.

Noldorin
A: 

If you need to know if the variable exists use Is/IsNot Nothing.

Using <> requires that the variable you're evaluating have the "<>" operator defined. Check out

 Dim b As HttpContext
 If b <> Nothing Then
    ...
 End If

and the resultant error

Error   1 Operator '<>' is not defined for types 'System.Web.HttpContext' and 'System.Web.HttpContext'.
hometoast
A: 

The C# and VB.NET compilers often generate different IL for operations that are apparently equivalent in both languages. It just so happens that C# does the "expected" thing when you write stringvar == null, but VB.NET does not. To get the same effect in VB.NET you have to force true reference equality with the Is operator.

Christian Hayter