[AccessD] FW: Class Module Method in Loop

Drew Wutka DWUTKA at marlow.com
Thu Mar 6 16:51:01 CST 2003


What is the Do Loop for, for the first example?
 
If you are just using a class object, you only need to declare it when
needed, and destroy it when you are done with it.
 
The exception to that rule, is that when you add a class to a collection,
you need to destroy it after you add it.....(before adding another that is.)
 
For example, let's say you have this as a class:
 
Public MyValue as Long
Function DoSomething()
'Do something with MyValue
End Function
 
If you wanted to have your class run through a for next loop, like this:
 
Dim cls as MyClass
Dim i as Long
Set cls = New MyClass
For i=1 to 100
    cls.MyValue=i
    cls.DoSomething
Next i
Set cls=Nothing
 
you only need to create the class before the loop, and destroy it at the
end, because you can work with just one instance of that class, instead of
creating and destroying a new instance of the class for each loop.
 
However, if you want to add a class to a collection:
 
Dim cls as MyClass
dim col as Collection
Dim i as Long
Set col= New Collection
For i=1 to 100
    Set cls = New MyClass
    cls.MyValue=i
    col.Add cls
    Set cls = Nothing
Next i
 
 
In the first example, putting the creation/destruction of a class within the
loop would just be a waste of resources.....it would still work though.  In
this example, you are adding a class to a collection, if you don't destroy
the class after you add it, adding it again would actual create a twin/clone
of that instance.  
 
Drew

-----Original Message-----
From: John W. Colby [mailto:jcolby at colbyconsulting.com]
Sent: Thursday, March 06, 2003 4:15 PM
To: AccessD
Subject: [AccessD] FW: Class Module Method in Loop


From: Myke Myers [mailto:mmmtbig at bellsouth.net] 
Sent: Wednesday, March 05, 2003 5:38 PM
To: 'accessd-admin at databaseadvisors.com'
Subject: Class Module Method in Loop


When I use a method of a class module in a loop, where should 'Set=' be? Or
should I be using a standard module for this kind of method?
 
Dim obj as clsTest
 
This:
 
Set obj = new clsTest
Do
    obj.Method...
Loop
Set obj = nothing
 
or This:
 
Do

    Set obj = new clsTest
    obj.Method...

    Set obj = nothing
Loop
 
 
TIA.
Myke
 
 
  _____  

Is email taking over your day? Manage your time with eMailBoss. Try it free!
http://www.eMailBoss.com <http://www.eMailBoss.com> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://databaseadvisors.com/pipermail/accessd/attachments/20030306/2e1f878b/attachment-0001.html>


More information about the AccessD mailing list