<> And Not In VB.NET

110,916

Solution 1

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 usage other than they make the most sense to me when reading back code.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
110,916
Dejan
Author by

Dejan

Updated on March 13, 2020

Comments

  • Dejan
    Dejan about 4 years

    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?