• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

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

Status
Not open for further replies.
Level 19
Joined
Jul 14, 2011
Messages
875
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,198
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