Ok, so I’m having a hard time coming up with a ‘good’ way to do the following:
Group 4 objects so that if I Object A I can easily and without having to go through Objects B & C to find out if Object D relates to Object A. As well as any other combination of referencing.
I am currently using SortedLists to link A to B and B to C and C to D and doing a really sloppy job of cross referencing them. This works, but its really, really ugly. I don’t like ugly code unless there is no other way to do it.
I’m thinking of using a class to store the objects. But what would be the best way to reference them?
Thanks in advance
More info:
The objects are controls on a form. The 4 different controls are all related to each other in that they correspond to the same setting that they are used to configure. There are multiple settings that get modified on the form, each using 4 controls.
I need a way to easily reference them without having to call Setting4ControlB.Checked or Setting2ControlD.text, thus the linked lists. I’m using generic events for certain controls in order to reuse code, they all do the same thing, except they reference different objects.
The ugly portion is first, setting up the links, and second, doing the referencing from A to D or C to A, etc. I wanted to be able to do something like this:
GetSettingControls(ReferencingControl).ControlA.Checked
ReferencingControl would be the sender in the event, ControlA is a control that is related to ReferencingControl.
First of all, if you have a relationship like A->B->C->D the only way to see if A relates to D is to go all the way from A to D. You can nontheless apply a few tricks
This will involve manually keeping track of each indirect reference present in the program, that is, you’ll be in charge of adding the A->D reference dinamically, that may be useful with certain logics like for example being able to ‘carry’ objects in a game.
You need to use a Dictionary of WeakReferences, so that you don’t keep objects alive through references…
that way if the gun is cleared from the object pool (for example the userObjects collection from your Level.Objects collection) the weakreference will become invalid.
You can check for the object by doing
This is just an example, you know, implement good accessors, properties and so on…