Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
lets say i have few instances of the class

public class AB
{
}

in main i create instances of class AB

AB a = new AB();
AB b = new AB();
AB c = new AB();
......


i want to map each class instance to an integer ID. How can i do that in c#?

What I have tried:

I tried enums. and dictionary. The problem with dictionary is that if i change the order of the instances, then even the ID changes. I want something like
if ID is 0, a should be used
if ID is 1, b should be used
if ID is 2, c should be used and so on

but i dont want to use if/else and switch
Posted
Updated 8-Jul-16 8:04am

You could use an array:
C#
private AB[] AllInstances = new AB[3];
...
    AB a = new AB();
    AllInstances[0] = a;
    AB b = new AB();
    AllInstances[1] = b;
    AB c = new AB();
    AllInstances[2] = c;
Then you just access the array by ID as an index:
C#
AB it = AllInstances[ID];

Alternatively, you could add the ID value to your AB class as a property, and add a static collection:
C#
private static List<AB> AllInstances = new List<AB>
Then in the AB constructor add it to the collection:
C#
public AB(int id)
   {
   ID = id;
   AllInstances.Add(this);
   }
All you need then is to use Linq to get the instance:
C#
public AB Get(int id)
   {
   return AllInstances.Where(ab => ab.ID == id).FirstOrDefault();
   }
 
Share this answer
 
"The problem with dictionary is that if i change the order of the instances, then even the ID changes."
That statement does not make sense. While it is correct that you should never rely on the KeyValuePairs in a Dictionary being in the same "order" you put them in (or, any order), using a Dictionary guarantees ... by virtue of the fact it does not allow duplicate Keys ... that each key is unique.

I think (guess) what you want is a unique id (int) for each instance of the Class; one way you can do that is:
C#
using System.Collections.Generic;

namespace YourNameSpace
{
    public class AB
    {
        private static int AB_ID = 0;

        public static Dictionary<AB, int> ABInstanceToID = new Dictionary<AB, int>();

        public int ID { set; get; }

        public AB()
        {
            this.ID = AB_ID;
            ABInstanceToID.Add(this, this.ID);
            AB_ID++;
        }
    }
}
So, given a run-time instance of AB, you can read its ID by doing a lookup in the static Dictionary:
C#
if(AB.ABInstanceToID.ContainsKey(someAB))
{
       int index = AB.ABInstanceToID[someAB];

       switch(index)
       {
           case 0:
              // handle case O
              break;

           default:
              break;
       }
}
else
{
     // throw an error ?
     // put up a warning with a MessageBox ?>
}
Note:

1.in "theory" the 'ContainsKey test should not be necessary, but I think it's always a good idea to check and re-check, and plan for errors.

2. while some might favor using Dictionary.TryGetValue for the check, in this case, where we can assume frequent attempts at look-up of missing Keys is very improbable, I see no reason to use that, here.
 
Share this answer
 
try this
C#
AB a = new AB();
AB b = new AB();
AB c = new AB();
AB d = new AB();
AB e = new AB();

AB[] array = new AB[5];
array[0] = a;
array[1] = b;
array[2] = c;
array[3] = d;
array[4] = e;

int id = 2;
var instance = array[id];  // gets instance for c
 
Share this answer
 
Why not use an array of your class then ID is the index to the class you want to access?

Following gives 4 classes.

AB[] a = new AB[4];

Then a[ID] access the class you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900