在 VBA 中将类型设置为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9788330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:47:42  来源:igfitidea点击:

Set a type in VBA to nothing?

vbaobjectvariablesuser-defined-types

提问by tyrex

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

and I want to delete all values of the variable pointat 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?

我想point一次删除变量的所有值。如果它是一个类,我只会使用Set point = New DataPoint, 或 set Set point = Nothing,但如果它是一种类型,我该如何继续?

回答by GSerg

You can benefit from the fact that functions in VB have an implicit variable that holds the result, and that contains the default type value by default.

您可以受益于以下事实:VB 中的函数具有保存结果的隐式变量,并且默认情况下包含默认类型值。

public function GetBlankPoint() as DataPoint
end function

Usage:

用法:

point = GetBlankPoint()

回答by Jean-Fran?ois Corbett

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

回答by Siddharth Rout

EDIT: Damn! Beaten by JFC :D

编辑:该死!被 JFC 击败 :D

Here is an alternative to achieve that in 1 line ;)

这是在 1 行中实现该目标的替代方法;)

Dim point As DataPoint
Dim emptyPoint As DataPoint

Public Type DataPoint
   list As Collection
   name As String
   number As Integer
End Type


Sub Sample()
    '~~> Fill the point
    Debug.Print ">"; point.name
    Debug.Print ">"; point.number
    point.name = "a"
    point.number = 25
    Debug.Print ">>"; point.name
    Debug.Print ">>"; point.number
    '~~> Empty the point
    point = emptyPoint
    Debug.Print ">>>"; point.name
    Debug.Print ">>>"; point.number
End Sub

SNAPSHOT

快照

enter image description here

在此处输入图片说明

回答by Brian Geniusz

Another option is to use the reserved word "Empty" such as:

另一种选择是使用保留字“Empty”,例如:

.number= Empty

.nu​​mber=空

The only issue is that you will need to change the number from integer to variant.

唯一的问题是您需要将数字从整数更改为变体。

回答by Unicco

One-liner:

单线:

Function resetDataPoint() As DataPoint: End Function

Usage:

用法:

point = resetDataPoint()

回答by xlNoobie

Using classes in VBA is usually a good practice in case it is not a single purpose solution or the class do not contain too many private attributes because if you want to adhere on OOP rules and keep your class safe, you should declare all the Let and Get properties for all private attributes of class. This is too much coding in case you have more than 50 private attributes. Another negative side of using classes in excel is fact, that VBA do not fully support the OOP. There is no polymorfism, overloading, etc.) Even you want to use an inheritance, you have to declare all the attributes and methods from the original class in the inherited class.

在 VBA 中使用类通常是一个很好的做法,以防它不是单一用途的解决方案,或者类不包含太多私有属性,因为如果您想遵守 OOP 规则并保证您的类安全,您应该声明所有 Let 和获取类的所有私有属性的属性。如果您有 50 个以上的私有属性,那么这种编码就太多了。在 excel 中使用类的另一个不利方面是事实,即 VBA 不完全支持 OOP。没有多态、重载等。)即使你想使用继承,你也必须在继承的类中声明原始类的所有属性和方法。

So in this case I would prefer the solution suggested by Jean-Fran?ois Corbett or GSeng, i.e. to assign an empty variable of the same UDT as the variable you want to clear or to use a function which to me seems little bit more elegant solution because it will not reserve permanent memory for the emtpy variable of your UDT type.

所以在这种情况下,我更喜欢 Jean-Fran?ois Corbett 或 GS​​eng 建议的解决方案,即分配一个与您想要清除的变量相同的 UDT 的空变量,或者使用一个对我来说似乎更优雅的函数解决方案,因为它不会为您的 UDT 类型的 emtpy 变量保留永久内存。

回答by Edgar Barrera

For that is better to use classes, you can declare a class module with the name of your type, then declare all of your members as public, then automatically you can set to nothing and new for create and delete instances.

为了更好地使用类,您可以使用您的类型名称声明一个类模块,然后将您的所有成员声明为 public,然后您可以自动设置为 nothing 和 new 以创建和删除实例。

syntax will be somthing like this after you create the class module and named like your type:

创建类模块并命名为您的类型后,语法将是这样的:

'
Public List as Collection
Public Name as String
Public Number as Long

Private Sub Class_Initialize()

'Here you can assign default values for the public members that you created if you want

End Sub