Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:vbnothing [2015/09/14] (current)
Line 1: Line 1:
 +======Nothing vs Empty vs Null ======
 +> **Developer:​** //​[[developer:​rhinoscript|RhinoScript]]//​
 +> **Summary:​** //Discusses what nothing means in VBScript.//
  
 +=====Overview=====
 +There has always been confusion about the semantics of data that are not even there. ​ Usually they'​ve written code something like:
 +
 +<code vb>
 +If varValue = Nothing Then
 +</​code>​
 + 
 +or
 + 
 +<code vb>
 +If varValue = Empty Then
 +</​code>​
 + 
 +or
 + 
 +<code vb>
 +If varValue = Null Then
 +</​code>​
 + 
 +Why does VBScript have **Null**, **Nothing** and **Empty**, and what are the differences between them?
 +
 +====Empty====
 +When you declare a variable in VBScript, the variable'​s value before the first assignment is undefined, or **Empty**.
 + 
 +<code vb>
 +Dim varValue ' Empty value
 +</​code>​
 +
 +So basically, Empty says //I am an uninitialized variant//. If you need to detect whether a variable actually is an empty variant and not a string or a number, you can use **IsEmpty**. ​ Alternatively,​ you could use **TypeName** or **VarType**,​ but I prefer **IsEmpty**.
 +
 +====Nothing====
 +Nothing is similar to Empty but subtly different. ​ Empty says //I am an uninitialized variant//, Nothing says //I am an object reference that refers to no object//. Objects are assigned to variables using the **Set** statement. Since the equality operator on objects checks for equality on the default property of an object, any attempt to say:
 +
 +<code vb>
 +If varValue = Nothing Then
 +</​code>​
 +
 +is doomed to failure -- Nothing does not have a default property, so this will produce a run-time error. To check to see if an object reference is invalid, use:
 +
 +<code vb>
 +If varValue Is Nothing Then
 +</​code>​
 +
 +====Null====
 +Null is more obscure. The semantics of Null are very poorly understood, particularly amongst people who have little experience with programming. ​ Empty says //I'm an uninitialized variant//, Nothing says //I'm an invalid object// and Null says //I represent a value which is not known.// Null is not True, not False, but Null! The correct way to check for Null is much as you'd do for Empty: use **IsNull** (or **TypeName** or **VarType**.)
 +
 +{{tag>​Developer RhinoScript}}
developer/scriptsamples/vbnothing.txt ยท Last modified: 2015/09/14 (external edit)