Hey,
If I have a list of objects, for example List<Cake>
, is it possible to have a private int key
in the Cake object that will store an auto-incremented value every time I add to the list.
My reason for wanting this is so that I can populate a combobox containing all the "Cakes" in my list with a unique value for each element that I don't have to input myself.
For example:
public class Cake
{
private int _id;
private string _name;
public Cake(string name)
{
_name = name;
}
public int GetID()
{
return _id;
}
}
List<Cake> myCakeList = new List<Cake>();
myCakeList.Add(new Cake("Sponge"));
myCakeList.Add(new Cake("Chocolate"));
myCakeList.Add(new Cake("Battenburg"));
myCakeList.ForEach(x => {Console.WriteLine(x.GetID());});
Ideally this code would return:
0000001
0000002
0000003
or (if I wanted a random id)
389hguhg907903
357fboib4969gj
fhgw90290682gg
Could you please advise if this is at all possible (for both key types) and how it can be done. Also, whether or not this is a terrible solution to my problem.
Many thanks in advance,
Ashley