Take the 2-minute tour ×

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 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?

share|improve this question
add comment

4 Answers

up vote 14 down vote accepted

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
share|improve this answer
Ok, thanks. I hoped that there would be a solution to this, other than defining a whole class or resetting all members individually. As regards to the blank-variable, that's a helpful idea. I will consider using it. –  FMan Mar 20 '12 at 14:07
+1 On Blank Data Point –  Siddharth Rout Mar 20 '12 at 14:22
@FMan: This is not defining a whole class... Just declaring one variable. –  Jean-François Corbett Mar 20 '12 at 19:28
yeah i know. the first sentence of my comment did not refer to this variable but was a general thought. –  FMan Mar 21 '12 at 11:04
add comment

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 DataPoint
end function


point = GetBlankPoint()
share|improve this answer
That's a nice one! –  FMan Mar 20 '12 at 14:13
+1 A Good one :) –  Siddharth Rout Mar 20 '12 at 14:23
add comment

EDIT: Damn! Beaten by JFC :D

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

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


enter image description here

share|improve this answer
add comment

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

.number= Empty

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

share|improve this answer
add comment

Your Answer


