Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "Null" and "Nothing" in VB6?

Tags:

vb6

I have a recordset like this:

Dim rs as Recordset
Set rs as New Recordset

'... a lot of coding ...

if Err.Number <> 0 Then ' oops, something gone wrong!
    If rs.State <> adStateClosed Then rs.Close
    Set rs = Nothing
end if

' I want to evaluate if rs is Nothing, or Null

if rs is Nothing then 
' this doesn't throw errors, and works well :D
end if

if rs is Null then
' this throws an error of "types not compatible"
end if

if rs = Null then
' this throws an error of "types not compatible"
end if

if isNull(rs) then
' never enters here, isNull(rs) evaluates to False
end if

I found out that in VB6 I rarely use "Null" (I used it for evaluating empty recordset schema names), but I use "Nothing" for stuff like images, adodb.connections or recordsets. For strings I have vbNullString. I read it is a pointer to a null string.

Is "Null" like a "unknown variable value" and "Nothing" a true null value?

like image 627
Broken_Window Avatar asked Apr 24 '14 15:04

Broken_Window


People also ask

Is null the same as nothing?

Null is a specific subtype of a Variant. It has no existence outside of the Variant type, and is created to allow a Variant to model a database null value. Nothing is a value of an Object variable. It essentially is identical to a null pointer, i.e. there is no object.

Is VB nothing same as null?

When checking whether a reference (or nullable value type) variable is null , do not use = Nothing or <> Nothing . Always use Is Nothing or IsNot Nothing . For strings in Visual Basic, the empty string equals Nothing .

What is null vb6?

In Visual Basic 6.0, the Null keyword indicated that a field contained no valid data, and the IsNull function was used to test for Null. In addition, Visual Basic 6 supported Null propagation when Null was used in an expression, the result of the expression would also be Null.

What is the difference between null and empty?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.


1 Answers

Null is a specific subtype of a Variant. It has no existence outside of the Variant type, and is created to allow a Variant to model a database null value.

Nothing is a value of an Object variable. It essentially is identical to a null pointer, i.e. there is no object.

The following raises an error because "Is" can only be used with Object variables:

if rs is Null then
' this throws an error of "types not compatible"
end if

The following raises an error because an Object variable can never be Null:

if rs = Null then
' this throws an error of "types not compatible"
end if

The following evaluates False because IsNull() takes a Variant argument.

if isNull(rs) then
' never enters here, isNull(rs) evaluates to False
end if

It is equivalent to:

VarType(rs) = vbNull
like image 63
Mark Bertenshaw Avatar answered Sep 23 '22 22:09

Mark Bertenshaw