Set a type in VBA to nothing? Set a type in VBA to nothing? vba vba

Set a type in VBA to nothing?


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.

public function GetBlankPoint() as DataPointend function

Usage:

point = GetBlankPoint()


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 = 0End With

Alternatively, you can create a "blank" variable and assign it to your variable each time you want to "clear" it.

Dim point As DataPointDim blank As DataPointWith point    Set .list = New Collection    .list.Add "carrots"    .name = "joe"    .number = 12End Withpoint = blank ' point members are now reset to default values


EDIT: Damn! Beaten by JFC :D

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

Dim point As DataPointDim emptyPoint As DataPointPublic Type DataPoint   list As Collection   name As String   number As IntegerEnd TypeSub 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.numberEnd Sub

SNAPSHOT

enter image description here