What is the difference between “Null” and “Nothing” in VB6?

十年热恋 提交于 2019-12-29 06:50:31

问题


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?


回答1:


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



回答2:


The following table explains how to use VBScript keywords.

Keywords Keyword Description
Empty The Empty keyword is used to indicate an uninitialized variable value. This is not the same thing as Null.

False The False keyword has a value equal to 0.

Nothing The Nothing keyword in VBScript is used to disassociate an object variable from any actual object. Use the Set statement to assign Nothing to an object variable. For example:

Set MyObject = Nothing

Several object variables can refer to the same actual object. When Nothing is assigned to an object variable, that variable no longer refers to any actual object. When several object variables refer to the same object, memory and system resources associated with the object to which the variables refer are released only after all of them have been set to Nothing, either explicitly using Set, or implicitly after the last object variable set to Nothing goes out of scope.

Null The Null keyword is used to indicate that a variable contains no valid data. This is not the same thing as Empty.

True The True keyword has a value equal to -1.



来源:https://stackoverflow.com/questions/23273301/what-is-the-difference-between-null-and-nothing-in-vb6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!