“如果不是 _____ 什么都不是”及其检查的内容

SQL*_*ner 2 vba

VB 中的这个语句是检查对象的存在,还是检查内容是否为空?我认为它正在检查内容是否为空,但我想再次检查。提前致谢。

Mat*_*don 9

VBA 中的“null”概念(在NullReferenceException-null的意义上,如果您熟悉 C# 或NullPointerException在 Java 中)包含在关键字中Nothing。这适用于 VB6(及更早版本)以及 VB.NET。

Dim foo As Object
Debug.Print foo.Bar ' boom, the dreaded runtime error 91 shows up
Run Code Online (Sandbox Code Playgroud)

所以你的解释Nothing是正确的。

与 Java 或 C# 相反,您不能使用比较运算符(==在 C#/Java 中,=在 VBA 中)在 VBA 中进行空检查,因此(以类似于 SQL 的方式),您使用Is关键字:

If foo Is Nothing Then ' if (foo == null) { }
Run Code Online (Sandbox Code Playgroud)

或者否定形式:

If Not foo Is Nothing Then ' if (foo != null) { }
Run Code Online (Sandbox Code Playgroud)

请注意,此公式无效,因为Nothing无法否定:

If foo Is Not Nothing Then ' incorrect formulation, if (foo == !null) { }
Run Code Online (Sandbox Code Playgroud)

事情变得泥泞和混乱,当你意识到VBA具有的IsNull功能....然后还IsEmpty和相应的NullvbNullEmptyvbEmpty值-但这些都是外面你的问题的范围,并在MSDN和堆栈溢出很容易找到。

  • 为了更好地理解,我更喜欢这样:**if Not (foo Is Nothing) then**。 (3认同)