如何在vba中构建可与0区分的默认空对象?
我正在读一个包含机械紧固件数据的文本文件。我通过具有以下属性的类构建了一个对象紧固件:如何在vba中构建可与0区分的默认空对象?,vba,excel,is-empty,Vba,Excel,Is Empty,我正在读一个包含机械紧固件数据的文本文件。我通过具有以下属性的类构建了一个对象紧固件: type as string number as long master as long slave as long 我想填充一组紧固件: Cfastener as collection 在文本文件中,类型、编号、主控和从控可以随机排列,并不总是全部存在。为了解决这个问题,我定义了一个当前紧固件类型的缓冲区和一个默认的空紧固件: currentfastener as fastener initfasten
type as string
number as long
master as long
slave as long
我想填充一组紧固件:
Cfastener as collection
在文本文件中,类型、编号、主控和从控可以随机排列,并不总是全部存在。为了解决这个问题,我定义了一个当前紧固件类型的缓冲区和一个默认的空紧固件:
currentfastener as fastener
initfastener as fastener
with initfastener
.type = "-1"
.number = -1
.master = -1
.slave = -1
end with
我读取文本文件,当我检测到一个关键字引用其中一个属性时,我测试当前紧固件中的值:
currentfastener as fastener
initfastener as fastener
with initfastener
.type = "-1"
.number = -1
.master = -1
.slave = -1
end with
到目前为止,我使用-1表示数字,使用-1表示字符串作为默认的空参数。到目前为止,它还可以,因为参数无法获得此值。但是现在,我想为主设备和从设备添加一个空间位置,它可以是-1。所以我想回到我的第一个想法,那就是把所有的参数都定义为空
但是,如果我没有弄错的话,就不可能在vba中区分0值和空值,这会造成麻烦
您知道一个默认值吗?它不是0,可以与0区分开来,也不是-1?Empty用于变量类型。将字符串或Long设置为空,然后用IsEmpty测试其空性是行不通的
但是,您可以使用变量来存储数据,然后可以安全地使用空值来表示空/缺失值
你是对的,VB会将空转换为0进行数值比较。例如:
Dim v As Variant
Debug.Print (v = 0) ' => True
但您可以使用VarType函数测试变量是否具有空值:
将所有这些都声明为变体
type as string
number as long
master as long
slave as long
声明后,其默认值将为空
你可以测试一下
Dim av As Variant
If IsEmpty(av) Then Debug.Print "if isempty(av ) "
' I Wouldn't use this (it works but is overkill)
' If VarType(av) = vbEmpty Then Debug.Print "If VarType(av)= vbEmpty "
您可以显式地分配空
av = Empty
VB将把空转换为0进行数值比较。谢谢您的解决方案。通常我不太喜欢variant,我更喜欢用正确的类型声明所有变量。但在这种情况下,我看不到其他解决方案,所以我将使用它。尽管如此,我不知道我是否会对所有参数使用variant:更容易理解和复制事实上我有更多的东西要读,或者只是对可能具有值-1的参数使用variant:可能更快,并且与类型相关的错误更少。我看看,我同意。我觉得使用变体很不舒服。使用VBA变量存储来自数据库表的数据时,如果表中的字段允许空值,则所有变量都需要容纳存储空值。所以它们都需要是变量,因为这是唯一存储NULL ie empty的数据类型。What a bugger.BTW Null与empty不同-如果您感兴趣,请参阅下面的答案谢谢此解决方案。通常我不太喜欢variant,我更喜欢用正确的类型声明所有变量。但在这种情况下,我看不到其他解决方案,所以我将使用它。尽管如此,我不知道我是否会对所有参数使用variant:更容易理解和复制事实上我有更多的东西要读,或者只是对可能具有值-1的参数使用variant:可能更快,并且与类型相关的错误更少。我看看。再次感谢。
Sub ExperimentsWithVariants()
Dim avar As Variant
' is not set
' When a variant has not been assigned
Debug.Print "-----------------------"
Debug.Print "NOT SET:"
Debug.Print "-----------------------"
Debug.Print "aVar = Empty ", (avar = Empty) ' True
Debug.Print "aVar", avar ' '' ie blank
Debug.Print "IsNull(aVar)", (IsNull(avar)) ' False
Debug.Print "IsEmpty(aVar)", (IsEmpty(avar)) ' True
Debug.Print "aVar = """"", (avar = "") ' True
Debug.Print "aVar = 0", (avar = 0) ' True
If avar = Empty Then
Debug.Print " "
Debug.Print "avar = Empty so the above would be the saem if you set avar = Empty explicitly"
Debug.Print " """
Else
avar = Empty
Debug.Print " "
Debug.Print "-----------------------"
Debug.Print " SET TO Empty"
Debug.Print "-----------------------"
Debug.Print "aVar = Empty ", (avar = Empty) ' True
Debug.Print "aVar", avar ' '' ie blank
Debug.Print "IsNull(aVar)", (IsNull(avar)) ' False
Debug.Print "IsEmpty(aVar)", (IsEmpty(avar)) ' True
Debug.Print "aVar = """"", (avar = "") ' True
Debug.Print "aVar = 0", (avar = 0) ' True
End If
avar = Null
Debug.Print " "
Debug.Print "-----------------------"
Debug.Print " SET TO NULL"
Debug.Print "-----------------------"
Debug.Print "aVar = Empty ", (avar = Empty) ' Null
Debug.Print "aVar", avar ' Null
Debug.Print "IsNull(aVar)", (IsNull(avar)) ' True
Debug.Print "IsEmpty(aVar)", (IsEmpty(avar)) ' False
Debug.Print "aVar = """"", (avar = "") ' Null
Debug.Print "aVar = 0", (avar = 0) ' Null
avar = ""
Debug.Print " "
Debug.Print "-----------------------"
Debug.Print " SET TO """""
Debug.Print "-----------------------"
Debug.Print "aVar = Empty ", (avar = Empty) ' True
Debug.Print "aVar", avar ' '' ie blank
Debug.Print "IsNull(aVar)", (IsNull(avar)) ' False
Debug.Print "IsEmpty(aVar)", (IsEmpty(avar)) ' False
Debug.Print "aVar = """"", (avar = "") ' True
Debug.Print "aVar = 0", (avar = 0) ' False
' Note
' Is empty returns false, whereas ="" returns NULL
End Sub
Sub ExperimentsWithVariants()
Dim avar As Variant
' is not set
' When a variant has not been assigned
Debug.Print "-----------------------"
Debug.Print "NOT SET:"
Debug.Print "-----------------------"
Debug.Print "aVar = Empty ", (avar = Empty) ' True
Debug.Print "aVar", avar ' '' ie blank
Debug.Print "IsNull(aVar)", (IsNull(avar)) ' False
Debug.Print "IsEmpty(aVar)", (IsEmpty(avar)) ' True
Debug.Print "aVar = """"", (avar = "") ' True
Debug.Print "aVar = 0", (avar = 0) ' True
If avar = Empty Then
Debug.Print " "
Debug.Print "avar = Empty so the above would be the saem if you set avar = Empty explicitly"
Debug.Print " """
Else
avar = Empty
Debug.Print " "
Debug.Print "-----------------------"
Debug.Print " SET TO Empty"
Debug.Print "-----------------------"
Debug.Print "aVar = Empty ", (avar = Empty) ' True
Debug.Print "aVar", avar ' '' ie blank
Debug.Print "IsNull(aVar)", (IsNull(avar)) ' False
Debug.Print "IsEmpty(aVar)", (IsEmpty(avar)) ' True
Debug.Print "aVar = """"", (avar = "") ' True
Debug.Print "aVar = 0", (avar = 0) ' True
End If
avar = Null
Debug.Print " "
Debug.Print "-----------------------"
Debug.Print " SET TO NULL"
Debug.Print "-----------------------"
Debug.Print "aVar = Empty ", (avar = Empty) ' Null
Debug.Print "aVar", avar ' Null
Debug.Print "IsNull(aVar)", (IsNull(avar)) ' True
Debug.Print "IsEmpty(aVar)", (IsEmpty(avar)) ' False
Debug.Print "aVar = """"", (avar = "") ' Null
Debug.Print "aVar = 0", (avar = 0) ' Null
avar = ""
Debug.Print " "
Debug.Print "-----------------------"
Debug.Print " SET TO """""
Debug.Print "-----------------------"
Debug.Print "aVar = Empty ", (avar = Empty) ' True
Debug.Print "aVar", avar ' '' ie blank
Debug.Print "IsNull(aVar)", (IsNull(avar)) ' False
Debug.Print "IsEmpty(aVar)", (IsEmpty(avar)) ' False
Debug.Print "aVar = """"", (avar = "") ' True
Debug.Print "aVar = 0", (avar = 0) ' False
' Note
' Is empty returns false, whereas ="" returns NULL
End Sub