VB6中"Null"和"Nothing"有什么区别?

Bro*_*dow 9 vb6

我有一个这样的记录集:

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
Run Code Online (Sandbox Code Playgroud)

我发现在VB6中我很少使用"Null"(我用它来评估空记录集模式名称),但是对于像images,adodb.connections或recordsets这样的东西我使用"Nothing".对于字符串我有vbNullString.我读到它是一个指向空字符串的指针.

"Null"是"未知变量值"而"Nothing"是真正的空值吗?

Mar*_*haw 17

Null是Variant的特定子类型.它不存在于Variant类型之外,并且创建它以允许Variant为数据库空值建模.

没有什么是Object变量的值.它基本上与空指针相同,即没有对象.

以下引发错误,因为"Is"只能与Object变量一起使用:

if rs is Null then
' this throws an error of "types not compatible"
end if
Run Code Online (Sandbox Code Playgroud)

以下引发错误,因为Object变量永远不能为空:

if rs = Null then
' this throws an error of "types not compatible"
end if
Run Code Online (Sandbox Code Playgroud)

以下计算False,因为IsNull()采用Variant参数.

if isNull(rs) then
' never enters here, isNull(rs) evaluates to False
end if
Run Code Online (Sandbox Code Playgroud)

它相当于:

VarType(rs) = vbNull
Run Code Online (Sandbox Code Playgroud)