Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<> And Not In VB.NET

Tags:

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?

like image 979
Kezzer Avatar asked Mar 16 '09 10:03

Kezzer


People also ask

What is the NOT operator in Visual Basic?

The Not Operator performs logical negation on a Boolean expression. It yields the logical opposite of its operand. If the expression evaluates to True , then Not returns False ; if the expression evaluates to False , then Not returns True .

What is the NOT operator?

What Does NOT Operator Mean? In Boolean algebra, the NOT operator is a Boolean operator that returns TRUE or 1 when the operand is FALSE or 0, and returns FALSE or 0 when the operand is TRUE or 1. Essentially, the operator reverses the logical value associated with the expression on which it operates.

What is not operator with example?

Similarly, if the condition's result is false or 0, the NOT operator reverses the result and returns 1 or true. For example, suppose the user enters a non-zero value is 5, the logical NOT (!) operator returns the 0 or false Boolean value.


1 Answers

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.

like image 84
Charlie Avatar answered Oct 20 '22 10:10

Charlie