我怎样才能build立一个默认的空对象,可以区分从VBA的0?

我正在阅读包含机械紧固件数据的文本文件。 我build立了一个对象紧固件(通过一个类)具有以下属性:

type as string number as long master as long slave as long 

我想填写一系列紧固件:

 Cfastener as collection 

在文本文件中,types,编号,主控和从属可以随机出现,并不总是存在。 为了处理这个问题,我定义了一个当前的紧固件(缓冲区)和一个默认的“空”紧固件:

 currentfastener as fastener initfastener as fastener with initfastener .type = "-1" .number = -1 .master = -1 .slave = -1 end with 

我读了我的文本文件,当我检测到引用这些属性的关键字之一时,我testing了current fastener的值:

 Do until .atendofstream line = .readline Select case line Case masterkeyword if currentfastener.master <> -1 then 'We already have a master. This means that we need to save the currentfastener and start a new one. Cfasteners.add currentfastener currentfastener = initfastener else 'master is "empty": we fill the currentfastener. currentfastener.master= "value read from the text stream" end if End Select Loop 

直到现在,我使用-1作为数字, "-1"作为默认的空参数。 到现在为止没有问题,因为参数不能得到这个值。 但现在,我想为主从机添加一个空间位置,它可以是-1 。 所以我想回到我的第一个想法,那就是将所有initfixation参数定义为empty

但是如果我没有弄错,就不可能区分vba中的0值和empty值,这会造成麻烦。

你知道一个默认值,不是0,可以区分从0和不是-1?

EmptyVarianttypes一起使用。 设置一个StringLongEmpty ,然后用IsEmpty()testing它的空白将不起作用。

但是, 可以使用变体来存储数据,然后可以安全地使用“ Empty值”来表示Empty值/缺失值。

你说得对,VB会将Empty0来进行数字比较。 例如:

 Dim v As Variant Debug.Print (v = 0) ' => True 

但是,您可以使用VarType()函数来testingvariables是否具有Empty值:

 Dim v As Variant Debug.Print VarType(v) = vbEmpty ' => True (empty/uninitialized) v = 0 Debug.Print VarType(v) = vbEmpty ' => False v = Empty Debug.Print VarType(v) = vbEmpty ' => True (empty) 

声明所有这些变体。

  type as string number as long master as long slave as long 

声明后的默认值是vbEmpty。

你可以testing这个

 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会将Empty设置为0来进行数字比较。

 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