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?
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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With