• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[C#] "Linking" 2(or more) objects to each other

Status
Not open for further replies.
Level 20
Joined
Jul 14, 2011
Messages
877
So, I started learning C# recently and needed a way to "link" 2 objects together so if I am given 1 of them, I can get its corresponding one.
For example, if I have a Button in a TabPage, and I am given one of them I can get the other.
The first thing that came to mind was putting them in an arrays with the same index, but I want to know if there is a better way of doing this.

Thanks in advance :)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
For 1:1 relationships a simple reference to its partner is sufficient.
For 1:N relationships you need an array, vector or list of references. Hashtables with named elements also work (how interpreters work) however since it resolves the location at runtime it is considerably slower than a direct address. Linked lists are fine for iteration but a vector is recommended if you want specific elements at known positions.
For sharing a pool of objects among each other it is inefficient to keep a list privately to each one. Instead use a container object (some implementations of lists are these) which you reference from each object. This means you have a N:1 relationship with the list object.
 
Status
Not open for further replies.
Top