Set a type in VBA to nothing?

后端 未结 7 1274
隐瞒了意图╮
隐瞒了意图╮ 2021-02-12 10:40

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 Ty         


        
7条回答
  •  野性不改
    2021-02-12 10:56

    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
    

提交回复
热议问题