PDA

View Full Version : Solved: Custom Classes and Collections



clew
07-02-2010, 07:05 AM
Hello. I've been working on a project with several custom collection classes, "nested" within one another. In other words, each custom class is a collection class, which contains instances of the next custom class. My custom classes are:

CSystem
CIMS
CSuite
CRouter
CTestArea

So, I have an instance of CSystem, called System. To it, I add an IMS, an instance of the CIMS class, and so on. In this manner, I have been able to multiplex, or nest the objects, and access them by using their indexes in their respective collections. For example, I can work with a Router by using:

System.IMS(1).Suite(3).Router(2).SomeProperty

I have created Add and Remove methods for each layer of the system. Each collection has a method to return the objects it contains. The Router method of the Suite object returns an object of class CRouter. I have also created methods to see if an object exists.

This has worked very well for me so far. But, for some reason, I have hit a wall with the deepest layer, the CTestArea. I have essentially set it up in the exact same way as the other classes. I added the same Add and Remove methods to the CRouter class as I have added to the higher-level classes. I have added the TestArea method, to return a CTestArea object of the argument index:

Public Function TestArea(Index As Variant) As CTestArea
TestArea = TestArea_Collection.Item(Index)
End Function

This follows the same format as the methods I've added to my other classes. But for some reason, I cannot get this to work. I get an Error 91: Object variable or with block variable not set any time I try to call a TestArea using an index:

System.IMS(i).SST(j).Router(k).Add "Room Sensor"
Dim l As Integer
l = System.IMS(i).SST(j).Router(k).Count
MsgBox (IsObject(System.IMS(i).SST(j).Router(k).TestArea(l)))

The MsgBox line produces the error (as does any other line that calls a TestArea). Note that I have verified everything is valid up to the TestArea method. After adding, the count is 1. I can even use the TestArea_Exists method to verify that TestArea(1) exists in the specified Router. I have no idea why this isn't working. The only thing that came to mind was wondering if there is some limit to how deep collections can be nested.


Sorry for the long post. Any thoughts would be helpful. Thanks in advance.

Bob Phillips
07-02-2010, 07:34 AM
Post a workbook, we'll never set it up correctly.

clew
07-02-2010, 08:07 AM
Attached is a simplified workbook, which exhibits the same issues.

Thanks!

Paul_Hossler
07-02-2010, 10:25 AM
in CRouter, you need to 'Set' the returning object


'*************
'TestArea method (returns CTestArea)
'*************
Public Function TestArea(Index As Variant) As CTestArea
Set TestArea = TestArea_Collection.Item(Index)
End Function


Paul

clew
07-02-2010, 10:54 AM
Yes! Thank you so much. It's amazing that I could look at it so many times and not see that.

I really appreciate the help.