• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[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,207
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