我找不到将var(Boolean)设置为空/ Null或其他平台然后True / False的方法 有没有办法告诉VB这个内存单元不会被使用?
答案 0 :(得分:5)
没有。 Variant
以外的内在类型不能设置为Null
。如果您需要可以为空的值,则需要将其声明为Variant
:
Sub Foo()
Dim x As Variant
x = Null
Debug.Print x
End Sub
这种方法的第一个缺点是,您需要在代码中的任何位置测试Null
的变量,然后再尝试将其分配给任何需要{{1}的任何过程。 }。否则,您将无法使用Null`运行时错误。
第二个缺点是你现在正在使用一个弱类型变量,这种变量违背了将其声明为Boolean
的目的。如果您真的需要表示Boolean
,True
或False
的类型,则可以选择声明您自己的枚举并使用:
Neither
最好以确保您的Private Enum Truthiness
IsTrue
IsFalse
IsNeither
End Enum
变量实际上代表二元条件的方式重构代码。
答案 1 :(得分:0)
一个人可以利用类 default属性和静态模块函数来创建一些支持Bool
,{{1 }} (均为单例), False_
个对象以及 True_
,Nothing
-因此以下会起作用(下面的实现):
isTrue()
isFalse()
Sub testBool()
'New Bool would throw an exception to ensure object equality for False_ and True_ values
Dim bool As Bool: Set bool = True_
'these 2 work only if you are sure its not 'Nothing'! (otherwise throw error):
If bool Then Debug.Print "1: true!"
Set bool = False_
If Not bool Then Debug.Print "2: false!"
Set bool = Nothing
If bool Is Nothing Then Debug.Print "3: Nothing!"
'these 2 work always even if "foo Is Nothing" (unassigned):
If isFalse(bool) Then Debug.Print "4: isFalse(Nothing) => true!"
If Not isTrue(bool) Then Debug.Print "5: not isTrue(Nothing) => true!"
'support for typical Strings, Integers as well:
If isTrue("1") And Not isTrue("0") Then Debug.Print "6: True ~ 1 and False ~ 0"
If isTrue("trUe") And Not isTrue("FalsE") Then Debug.Print "7: True ~ UCase('trUe') and False ~ UCase('FalsE')"
End Sub
Sub unittestBool()
'usage of object False_ and True_ in boolean evaluation (by default property usage)
Debug.Assert False_ = False 'False_.native is the default property which is "False"
Debug.Assert True_ = True 'True_.native is the default property whcih is "True"
'test with Bool variable
'test for unknown/nothing/null:
Debug.Assert bool Is Nothing 'unassigned and usable as kind of "Null" as in other languages like Java
Set bool = False_ 'False_ is a singleton (static) function
Debug.Assert bool = False 'bool.native is the default property which is "False"
Set bool = True_ 'True_ is a singleton (static) function
Debug.Assert bool = True 'bool.native is the default property which is "True"
'test True_(Variant) function: with Bool object
Debug.Assert isTrue(bool)
Set bool = False_
Debug.Assert Not isTrue(bool)
'test False_(Variant) function: with Bool object
Debug.Assert isFalse(bool)
Set bool = True_
Debug.Assert Not isFalse(bool)
'test True_(Variant) function: with Integer object
Debug.Assert isTrue(1)
Debug.Assert Not isTrue(0)
'test True_(Variant) function: with String object
Debug.Assert isTrue("true") 'case insensitive
Debug.Assert isTrue("True")
Debug.Assert isTrue("trUe")
Debug.Assert Not isTrue("false")
Debug.Assert isTrue("1")
Debug.Assert Not isTrue("0")
End Sub
类(Bool.cls
)的实现:
(行Bool
很重要,必须从Bool.cls
文件(!)中 import it,而不仅仅是创建它!)
Attribute native.VB_UserMemId = 0
Bool.cls
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Bool"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private val As Boolean
Property Get native() As Boolean
Attribute native.VB_UserMemId = 0
native = val
End Property
Property Let native(value As Boolean): val = value: End Property
Private Sub Class_Initialize()
If Bool_initialized Then err.raise 1, "Bool", "usage of New Bool forbidden. Use assignment of existing True_ and False_ objects only!"
End Sub
模块(BoolMod.bas
)的实现:
BoolMod