valkyrie
January 26th, 2000, 05:08 AM
Hi,
I am a bit confused about collections in VB. Can someone explain the difference between collections and classes in VB?
Thanks for your reply(s)
____________________________________
The VB Bugs in my Life...
Chris Eastwood
January 26th, 2000, 07:53 AM
A collection is simply a collection of 'things' - that can be either variants or classes.
A class is a 'blue-print' for an object (it doesn't become an object until you do a 'set x = new ....' on it).
A collection can store references to objects internally and can be used to retrieve these object references by a key.
eg.
'
Dim oCollection as Collection
Dim oCtl as Control
'
set oCollection = new Collection
'
for Each oCtl In me.Controls
oCollection.Add oCtl, oCtl.Name
next
'
for Each oCtl In oCollection.Controls
Debug.print oCtl.Name
next
'
' Display properties of 'Command1'
'
set oCtl = oCollection("Command1")
'
Debug.print oCtl.Name
Debug.print oCtl.Top
Debug.print oCtl.Left
'
The above code goes through all the controls stored on a form and then adds them to another collection object that we create. The code then looks for the 'Command1' control inside our collection and then assigns an object variable to it, it then accesses that object's properties.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb