在VB6中,“Null”和“Nothing”的区别是什么?

9

我有这样的记录集:

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

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

"Null"是否像"未知变量值",而"Nothing"是真正的null值?

3个回答

19

Null是Variant的一个具体子类型。它在Variant类型之外没有存在,被创建是为了允许Variant对数据库中的空值进行建模。

Nothing是Object变量的一个值。它本质上与null指针相同,即没有对象。

以下代码会引发错误,因为“Is”只能用于Object变量:

if rs is Null then
' this throws an error of "types not compatible"
end if
以下代码会因为一个对象变量永远不可能是Null而抛出错误:
if rs = Null then
' this throws an error of "types not compatible"
end if
以下表达式的结果为False,因为IsNull()函数需要一个Variant类型的参数。
if isNull(rs) then
' never enters here, isNull(rs) evaluates to False
end if

它的含义等同于:

VarType(rs) = vbNull

1
还有一个变量值是 Empty - Bob77

0

下表解释了如何使用VBScript关键字。

  • Empty

    Empty关键字用于指示未初始化的变量值。这与Null不同。

  • False

    False关键字的值等于0。

  • Nothing

    在VBScript中,Nothing关键字用于将对象变量与任何实际对象分离。使用Set语句将Nothing分配给对象变量。例如:

Set MyObject = Nothing

几个对象变量可以引用同一个实际对象。当将 Nothing 分配给对象变量时,该变量不再引用任何实际对象。当几个对象变量引用同一对象时,与这些变量引用的对象相关联的内存和系统资源仅在它们全部被设置为 Nothing 后才会释放,可以通过使用 Set 显式地释放,也可以在最后一个设置为 Nothing 的对象变量超出范围后隐式释放。

  • Null

    Null 关键字用于指示变量不包含有效数据。这与 Empty 不同。

  • True

    True 关键字的值等于 -1。


-1
您可以尝试以下操作:
a = "SELECT SBio.biosrno,YearlyCharges.adnum,Name,sName
FROM SBio RIGHT JOIN YearlyCharges ON SBio.biosrno=YearlyCharges.adnum
WHERE  (SBio.biosrno IS  NULL );"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接