I have a class (Deck). One of its properties is an array of another class (Rib). I have an instance of this Deck class - Dk.
I want Rib to be able to access some other property of Deck. So I want Rib to “know” the instance of the Deck class of which it is in a property array.
So I can imagine a property of Rib being an instance of the Deck class. Specifically the instance of which is it (the Rib) is a property.
So I imagine that in the Constructor of the Dk instance I would create all the Ribs and set a property of the Rib class to “myself” as it were.
What is the syntax for that?
Alternatively, how should I be approaching this problem of a class instance (A) that is the property of a separate class instance (B) “knowing” the class instance of which it is a property. I want ( A) to have access to other properties of (B)
Use Me. However, be aware that this creates a circular reference so you have to be a little more careful with disposing of the instances. You have to break the circular reference explicitly before you get rid of the instances. One way would be to redim the array of ribs to -1 before you dispose of the deck instance.
Thanks for the answer, particularly for the warning about the circular reference.
I may chicken out and keep the Deck properties that I need in some global structure, perhaps a Dictionary, that all the contained property classes such as Rib and the Deck instance itself can refer to as necessary. In this particular project, there is only one Deck instance in existence.
But if I am feeling bolder and more “class oriented” in this or other projects I will use Me.
or dont do that at all and keep “rib” completely in the dark about what owns it
have the rib instances ASK for the deck using an event
Class Rib
event def WhatDeck() as Deck
public sub whoOwnsMe()
dim d as Deck = WhatDeck()
break
end sub
End Class
Class Deck
private property mRibs() as Rib
public sub addRib( ribInstance as Rib)
addhandler ribInstance.WhatDeck, addressof DeckResponder
mRibs.append ribInstance
end sub
public sub removeRib( ribInstance as Rib)
dim idx as integer = mRibs.indexOf(ribInstance)
if idx >= 0 then
removehandler ribInstance.WhatDeck, addressof DeckResponder
mRibs.remove idx
end if
end sub
Private Sub DeckResponder( instance as Rib ) as Deck
return self
end sub
End Class