Set Class1Var = New Class1

D

Dave D-C

Hello,
I'm wondering about using "Set Class1Var = New Class1".
This routine needs one occurance of that statement, but it doesn't
seem to hurt to execute it millions of times. (I know I could've said
Dim Class1Var as New Class1).

Q - What's going on when I execute that statement the 2nd, 3rd, etc.
time? TIA, D-C

Dim Class1Var As Class1

Sub Sub1()
Dim iLong&
' Set Class1Var = New Class1 ' once here
For iLong = 1 To 1000000000
' Set Class1Var = New Class1 ' many times here
Set Class1Var.WithEventsImg = UserForm1.Image1
Class1Var.WithEventsImg.Tag = Format$(iLong, "000,000,000")
If iLong Mod 10000 = 0 Then ' every once in awhile
StatusBar = Class1Var.WithEventsImg.Tag
DoEvents
End If
Next iLong
End Sub

'Class1 Module for reference
'Public WithEvents WithEventsImg As MSForms.Image
'Private Sub WithEventsImg_MouseDown( _
' ByVal Button As Integer, ByVal Shift As Integer, _
' ByVal X As Single, ByVal Y As Single)
' MsgBox WithEventsImg.Tag
'End Sub
 
B

Bill Renaud

From the "Set Statement" in Excel Visual Basic Help:

"New is usually used during declaration to enable implicit object
creation. When New is used with Set, it creates a new instance of the
class. If objectvar contained a reference to an object, that reference
is released when the new one is assigned."

What you are doing is allocating a memory block to contain the object
"Class1", then causing (setting) the variable "Class1Var" to point to
that oject. Since the memory block that "Class1Var" previously pointed
to is now no longer being pointed to by anything, the memory allocation
routine in VB will delete (deallocate) it. This is pointless and only
causes your routine to run slower. It may even crash eventually,
depending on how robust the memory allocation process works.
 
D

Dave D-C

Dave D-C said:
Thanks
[That was a Word version -
replace "statusbar" with "application.statusbar"]

Bill Renaud said:
From the "Set Statement" in Excel Visual Basic Help:

"New is usually used during declaration to enable implicit object
creation. When New is used with Set, it creates a new instance of the
class. If objectvar contained a reference to an object, that reference
is released when the new one is assigned."

What you are doing is allocating a memory block to contain the object
"Class1", then causing (setting) the variable "Class1Var" to point to
that oject. Since the memory block that "Class1Var" previously pointed
to is now no longer being pointed to by anything, the memory allocation
routine in VB will delete (deallocate) it. This is pointless and only
causes your routine to run slower. It may even crash eventually,
depending on how robust the memory allocation process works.
 
C

Chip Pearson

If you don't store a reference to each class instance in an object like a
Collection or Dictionary, VBA will create and destroy the classes as the
variable is reused. For example, put the following in Class1:

Public Num As Long
Private Sub Class_Initialize()
Debug.Print "init: " & CStr(ObjPtr(Me))
End Sub

Private Sub Class_Terminate()
Debug.Print "term: " & CStr(ObjPtr(Me)) & " Num: " & CStr(Num)
End Sub

Then in Module1, use

Sub AAA()
Dim C As Class1
Dim N As Long

For N = 1 To 5
Set C = New Class1
C.Num = N
Next N
End Sub

You'll see that the classes are destroyed as C is reused. You can also force
the destruction of the classes with:

Sub BBB()
Dim C As Class1
Dim N As Long

For N = 1 To 5
Set C = New Class1
C.Num = N
Set C = Nothing
Next N
End Sub

However, if you store a reference in a Collection, the class instances are
not reused and are not destroyed until the Collection is destroyed:

Dim Coll As New Collection

Sub BBB()
Dim C As Class1
Dim N As Long

For N = 1 To 5
Set C = New Class1
C.Num = N
Coll.Add C
Next N
Set Coll = Nothing
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
D

Dave D-C

Wow, that is didactic!
I refuse to dispense with "option explicit", so I
figured out to use "Dim Coll as New Collection".
(I've never used a collection before)

ObjPtr is not in my (XL97) help.
Looking at search.microsoft.com:
"It is uncommon for a Visual Basic programmer to need to obtain low
level information on a variable, such as its memory address. However,
there are some API functions that require such information. This
article describes the following Visual Basic functions that may help a
Visual Basic programmer obtain this information: .. "
This looks like the beginning of getting into big trouble :)

Thanks, D-C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top