Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET language and Nothing : why is it the way it is?

This is a question about the VB.NET language. Since I am using it every day, I just try to understand the motivations behind some of its constructs.

I just find out that this line :

If myObject Is Nothing then

is as correct as this one is :

If Nothing Is myObject Then

Same results. Using ildasm, we can see that these lines are translated to :

if myObject = null then

and

if null = myObject then

Well, but, in VB.NET, you cannot write :

if myObject = Nothing Then

The compiler will not accept that.

Mmm, to me, If Nothing Is myObject is much more less obvious than If myObject = Nothing.

Why did VB.NET authors just think the opposite ? Any hint ?

like image 200
Sylvain Rodrigue Avatar asked Jun 23 '09 19:06

Sylvain Rodrigue


People also ask

What is VB.NET language?

Visual Basic, originally called Visual Basic . NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on . NET, Mono, and the . NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visual Basic language, the last version of which was Visual Basic 6.0.

Is Visual Basic a dead language?

Visual Basic (VB.NET) will continue to be supported by Microsoft. (It's not dead.) The language will no longer have new features added to it. (It's done.)

What does <> mean in Visual Basic?

<> in VB.NET means "not equal to". It can be used with the normal oprands as well as in comparision with the items when compared with the datas fetched with the data reader (from database). Follow this answer to receive notifications.

Why do people use Visual Basic?

Visual basic programming language allows programmers to create software interface and codes in an easy to use graphical environment. VB is the combination of different components that are used on forms having specific attributes and actions with the help of those components.


2 Answers

The problem you're running into is that VB.Net differentiates the 2 types of object comparison. Namely Reference and Value comparison.

The "Is" operator in VB.Net is used for reference comparison. This can be used when the values in question are both reference types or nullables. Attempting to compare value types i this manner will result in a compilation error.

The "=" operator is used for Value comparison. The value comparison can only be used on types which define an explicit =, <> operator pair in their class definition. The actual implementation of the equality depends on the implementation of the operator.

C# takes a different approach in that it uses == for both value and reference comparison. Which is used is dependent upon a couple of factors including the type of the values being compared and the implementation of certain equality methods.

like image 165
JaredPar Avatar answered Jan 10 '23 20:01

JaredPar


It is one of these things inherited from VB6 and COM. VB6 makes a distinction between reference type objects (that are instantiable) and native types such as int. Reference types had to be created and assigned with the "Set" operator whereas native types simply used "=".

like image 38
Otávio Décio Avatar answered Jan 10 '23 22:01

Otávio Décio