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?
This question is related to
vb.net
operators
query-string
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.
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.
I'm a total noob, I came here to figure out VB's 'not equal to' syntax, so I figured I'd throw it in here in case someone else needed it:
<%If Not boolean_variable%>Do this if boolean_variable is false<%End If%>
in fact the Is is really good, since to the developpers, you may want to override the operator ==, to compare with the value. say you have a class A, operator == of A is to compare some of the field of A to the parameter. then you will be in trouble in c# to verify whether the object of A is null with following code,
A a = new A();
...
if (a != null)
it will totally wrong, you always need to use if((object)a != null)
but in vb.net you cannot write in this way, you always need to write
if not a is nothing then
or
if a isnot nothing then
which just as Christian said, vb.net does not 'expected' anything.
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
.
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'.
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.
Just use which sounds better. I'd use the first approach, though, because it seems to have fewer operations.
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.
Source: Stackoverflow.com