Vb.net – <> And Not In VB.NET

operatorsquery-stringvb.net

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?

Best Solution

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.