在 VBA 中将类型设置为空? [英] Set a type in VBA to nothing?
问题描述
我用自己的类型定义了一个变量,比如
I have defined a variable with an own type, say
Dim point As DataPoint
Public Type DataPoint
list as Collection
name as String
number as Integer
End Type
并且我想一次删除变量 point
的所有值.如果是一个类,我只会使用Set point = New DataPoint
,或者设置Set point = Nothing
,但是如果它是一个类型,我该如何继续?
and I want to delete all values of the variable point
at once. If it was a class, I would just use Set point = New DataPoint
, or set Set point = Nothing
, but how can I proceed if it's a type?
推荐答案
标准方法是将每个成员单独重置为其默认值.与对象相比,这是用户定义类型的限制之一.
The standard way is to reset each member to its default value individually. This is one limitation of user-defined types compared to objects.
冒着陈述显而易见的风险:
At the risk of stating the obvious:
With point
Set .list = Nothing
.name = ""
.number = 0
End With
或者,您可以创建一个空白"变量,并在每次要清除"它时将其分配给您的变量.
Alternatively, you can create a "blank" variable and assign it to your variable each time you want to "clear" it.
Dim point As DataPoint
Dim blank As DataPoint
With point
Set .list = New Collection
.list.Add "carrots"
.name = "joe"
.number = 12
End With
point = blank
' point members are now reset to default values
这篇关于在 VBA 中将类型设置为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!