Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classes and Collections 2

Status
Not open for further replies.

rdavis

Programmer
Apr 29, 2002
157
US
Hi,
I'm trying to grasp the concept of collections and/or classes. Looking in the archives did not help me to much. What I'm looking to do is take a group of controls on a form, not all of them, and send them to a function. These controls are also in an array. Am I going the right way by looking into collections and classes. If so, can someone help me with a little sample to help me get started.

Thank you Rob
 
Since a collection can contain anything that you can assign to a variant, you can easily place a control into a collection and pass the collection to a function.

Just add the item to the collection, with or without a key as needed, and pass it in the statement that calls the function. In the function you can act on whatever is in the collection, and then return.

The fact that the controls are also in an array does not matter. They can exist in a collection at the same time.

After the line that called the function you can clear the collection, or keep it for the next call, depending on what you need.

For example, in one of my projects I have a collection of controls that I want to move, depending on user input, so I have a collection for them:

Public MoveableControls As New Collection

Then as I dynamically add controls to the worksheet, I add to the collection as necessary ( NextControl is the next index # in my array for this control ):

MoveableControls.Add ControlPointButton NextControl), "ControlPointButton(" & NextControl & ")"

So then, I can use the collection to loop through all of the controls ( of different types ), to move around when a scroll bar is changed. This isn't a "function", but it's the same basic idea:

Private Sub VScroll1_Change()
Dim Change As Integer
Dim Item As Control

Change = OffsetY - VScroll1.Value
OffsetY = OffsetY - Change

For Each Item In MoveableControls
Item.Top = Item.Top + Change
Next

End Sub


That's basically it!

HTH,

Robert
 
A very simplistic view is that a Class is a single object with properties, methods, and events. A collection is, for lack of a better term, a container where you can put multiple instances of item together in a group.

A collection can contain several instances of a particular class object. A class can have has one of its properties, a collection of some other type of object.

Lets assume that you have an Customer. You can establish a Customer class where you maintain all of your customer information. Each instance of the Customer Class represents one customer.

Let assume that you have an Order class. You establish an order class which contains all of the information about a particular order.

Given that an individual customer can place many orders, you might want to add a property to the Customer class, which is a Orders Collection, where each element in the collection is an instance of one specific order made by that customer.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Robert,

That is basically what I need collections for. I worked on this all day yesterday and now I understand collections a lot more, as for classes, I need a little more time. Thanks for all your help guys.

Rob
Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top