tags:

views:

119

answers:

4

What happens when i set a variable to nothing in VB.NET? Is it true that nothing equals to default, or am i missing something here?

A: 

It equals default on ValueTypes or Structs, and is equal to null on Object types.

Aequitarum Custos
+3  A: 

If it's a value type (like Integer, Double, etc.) setting the variable to Nothing will set it to the default value.

If it's a reference type, it will really be set to Nothing (null value).

In Microsoft's words:

Assigning Nothing to a variable sets it to the default value for its declared type.

If the variable is of a reference type, a value of Nothing means that the variable is not associated with any object. The variable has a null value.

Meta-Knight
A: 

Assuming VB.NET is largely similar to C#, null, which is called Nothing in VB.NET means that a reference doesn't point to anything. All types have default values when they are declared but not assigned: for example, the default value for ints is 0. The default value for reference types is the null value Nothing. So an unassigned variable of a reference type will have the value Nothing (null).

Joe
A: 

There's good blog Article on Null vs Nothing by Eric Lippert

Nothing does not equal default in all cases.

John Knoeller
-1. It's a great article but it's about **VBScript** and the question is about **Visual Basic.Net**. They behave completely differently - VBScript only has `Variants` not strongly typed variables.
MarkJ
@Mark: and yet the answer is the same wither it's VB.Net or VBScript.
John Knoeller