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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing a class module within another class module

Status
Not open for further replies.

BayouC

Technical User
Jan 16, 2009
2
US

I have two separate class modules - and I'd like to have one of those modules as a property of the other module (so I can reference for example module1.module2.property2). I did something that appeared initially to work, until I tried to build a collection. I build a collection of instances of module1, however when I go to reference collection.item(1).module2.property2 - all I get is the last value I entered. Meaning, if I had 5 items in the collection, while all my module1 properties reference the values I entered for each of the 5, my module1.module2 properties reference only item(5).

Am I trying to do something which is simply not possible?

Here is how I built the class (probably incorrectly) - module1 is called CContact, module2 is CMission. In the CContact module I defined a property:

Private: m_mission as New CMission

with:

Property Get Mission() as CMission
set Mission = m_mission
End Property
Property Let Mission (ref as CMission)
set m_mission = ref
End Property

Can this be done? (am I breaking some cardinal rule of programming by even attempting this type of reference? - I'm not a programmer)
 
Your code looks fine - it's perfectly normal to have a class be a property of another class.

Your problem is probably how you are building the collection. I suspect all your Module1's are pointing to the same instance of Module2. I suspect you are doing something like this:

Code:
Dim MyModule2 As New Module2

collection.item(1).Mission = MyModule2
collection.item(2).Mission = MyModule2
collection.item(3).Mission = MyModule2
collection.item(4).Mission = MyModule2
collection.item(5).Mission = MyModule2

What is happening above is that every item in the collection is pointing to the same MyModule2. The change below would give each collection item it's own Module2 object:
Code:
Dim MyModule2 As Module2

Set MyModule2 = New Module2
collection.item(1).Mission = MyModule2
Set MyModule2 = New Module2
collection.item(2).Mission = MyModule2
Set MyModule2 = New Module2
collection.item(3).Mission = MyModule2
Set MyModule2 = New Module2
collection.item(4).Mission = MyModule2
Set MyModule2 = New Module2
collection.item(5).Mission = MyModule2



Joe Schwarz
Custom Software Developer
 
There is no Get property with a class.

Property Set Mission() as CMission
 
I meant to say there is no Let. You should have a Set and a Get.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top