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
Bill Renaud - 14 Sep 2007 06:05 GMT
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.

Signature
Regards,
Bill Renaud
Dave D-C - 14 Sep 2007 13:56 GMT
Thanks
[That was a Word version -
replace "statusbar=" with "debug.print"]
>From the "Set Statement" in Excel Visual Basic Help:
>
[quoted text clipped - 10 lines]
>causes your routine to run slower. It may even crash eventually,
>depending on how robust the memory allocation process works.
Dave D-C - 14 Sep 2007 14:01 GMT
>Thanks
>[That was a Word version -
[quoted text clipped - 18 lines]
>http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
>----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Chip Pearson - 14 Sep 2007 14:50 GMT
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

Signature
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
> Hello,
> I'm wondering about using "Set Class1Var = New Class1".
[quoted text clipped - 35 lines]
> ----= East and West-Coast Server Farms - Total Privacy via Encryption
> =----
Dave D-C - 15 Sep 2007 00:01 GMT
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
>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
[quoted text clipped - 51 lines]
> Set Coll = Nothing
>End Sub
Dave D-C - 15 Sep 2007 00:05 GMT
Ignore the
Dim Coll as new collection
comment. I just didn't copy it.