Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default Set Class1Var = New Class1

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




----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default Set Class1Var = New Class1

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.
--
Regards,
Bill Renaud



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default Set Class1Var = New Class1

Thanks
[That was a Word version -
replace "statusbar=" with "debug.print"]

"Bill Renaud" wrote:
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.



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default Set Class1Var = New Class1

Dave D-C wrote:

Thanks
[That was a Word version -
replace "statusbar" with "application.statusbar"]

"Bill Renaud" wrote:
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.



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Set Class1Var = New Class1

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)








"Dave D-C" wrote in message
...
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




----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default Set Class1Var = New Class1

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

"Chip Pearson" wrote:
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



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default Set Class1Var = New Class1

Ignore the
Dim Coll as new collection
comment. I just didn't copy it.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:28 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"