Message 1 of 12
classes, collections, & dictionaries, oh my!
Not applicable
09-04-2001
12:24 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've been diligently working and studying all weekend trying to create a
class module that serves as a wrapper for a collection, or more likely a
dictionary, object. I'm having a few problems and I'm thinking that I may
simply be implementing my ideas incorrectly. I want to create a small
object model like so:
WorkingStates
|
|__WorkingState
Where WorkingStates is a collection, or dictionary, containing WorkingState
objects similar to the Layers--Layer relationship in the acad model. What
I've done is to create a project with two class modules named WorkingStates
& WorkingState. I'm storing the info to populate these objects in the
registry. The code to retrieve this info is working correctly. What I need
to know is what is the proper method for populating these objects. I want
to be able to work with a single working state,
WorkingStates.Item("WD").MyCoolMethod(I realize that I must provide the Item
method myself), & also be able process all WorkingState objects with a For
Each WorkingState In WorkingStates loop. My first thought was to populate
the WorkingStates object in the class_Initialize() event with something like
this:
Option Explicit
Private colWorkingStates As Collection
Private Sub Class_Initialize()
Dim mobjWorkingState As WorkingState
Dim tmpArray As Variant
Dim i As Integer
'Get all working states saved in registry
tmpArray = GetAllSettings("WorkingStates", "States")
'populate collection
Set colWorkingStates = New Collection
For i = LBound(tmpArray) To UBound(tmpArray)
Set mobjWorkingState = New WorkingState
colWorkingStates.Add Item:=mobjWorkingState.Item(tmpArray(i, 0)), _
Key:=tmpArray(i, 0)
Set mobjWorkingState = Nothing
Next i
'set properties
lngCount = colWorkingStates.Count
End Sub
Private Sub Class_Terminate()
Set colWorkingStates = Nothing
End Sub
My concern here is that this will run through code and populate every
WorkingState object stored in the registry, even if I only want to work with
a single WorkingState. Is there another way? I have put the code to
populate the WorkingState object in the Item method of the WorkingState. If
anyone has an outline of how this is normally done, I'd appreciate any help
at all 🙂
My next problem is that I'm having trouble implementing a For Each loop
using these objects.
Dim objWSS as WorkingStates
Dim objWS as WorkingState
Set objWSS = New WorkingStates
For Each objWS in objWSS
*DoStuff*
Next objWSS
My reference material states that we must provide an interface for the For
Each...Next loop by creating this property:
Public Property Get NewEnum() As IUnknown
Set NewEnum = colWorkingStates.[_NewEnum]
End Property
Which I've obviously done, but can't seem to get the loop to work. Thanks
to all for your help.
--
Bobby C. Jones
class module that serves as a wrapper for a collection, or more likely a
dictionary, object. I'm having a few problems and I'm thinking that I may
simply be implementing my ideas incorrectly. I want to create a small
object model like so:
WorkingStates
|
|__WorkingState
Where WorkingStates is a collection, or dictionary, containing WorkingState
objects similar to the Layers--Layer relationship in the acad model. What
I've done is to create a project with two class modules named WorkingStates
& WorkingState. I'm storing the info to populate these objects in the
registry. The code to retrieve this info is working correctly. What I need
to know is what is the proper method for populating these objects. I want
to be able to work with a single working state,
WorkingStates.Item("WD").MyCoolMethod(I realize that I must provide the Item
method myself), & also be able process all WorkingState objects with a For
Each WorkingState In WorkingStates loop. My first thought was to populate
the WorkingStates object in the class_Initialize() event with something like
this:
Option Explicit
Private colWorkingStates As Collection
Private Sub Class_Initialize()
Dim mobjWorkingState As WorkingState
Dim tmpArray As Variant
Dim i As Integer
'Get all working states saved in registry
tmpArray = GetAllSettings("WorkingStates", "States")
'populate collection
Set colWorkingStates = New Collection
For i = LBound(tmpArray) To UBound(tmpArray)
Set mobjWorkingState = New WorkingState
colWorkingStates.Add Item:=mobjWorkingState.Item(tmpArray(i, 0)), _
Key:=tmpArray(i, 0)
Set mobjWorkingState = Nothing
Next i
'set properties
lngCount = colWorkingStates.Count
End Sub
Private Sub Class_Terminate()
Set colWorkingStates = Nothing
End Sub
My concern here is that this will run through code and populate every
WorkingState object stored in the registry, even if I only want to work with
a single WorkingState. Is there another way? I have put the code to
populate the WorkingState object in the Item method of the WorkingState. If
anyone has an outline of how this is normally done, I'd appreciate any help
at all 🙂
My next problem is that I'm having trouble implementing a For Each loop
using these objects.
Dim objWSS as WorkingStates
Dim objWS as WorkingState
Set objWSS = New WorkingStates
For Each objWS in objWSS
*DoStuff*
Next objWSS
My reference material states that we must provide an interface for the For
Each...Next loop by creating this property:
Public Property Get NewEnum() As IUnknown
Set NewEnum = colWorkingStates.[_NewEnum]
End Property
Which I've obviously done, but can't seem to get the loop to work. Thanks
to all for your help.
--
Bobby C. Jones