vendredi 15 avril 2016

Creating an instance of a class with a name held in a string

I'm looking for a way to initialize a new instance of a class with a string as its name in order for me to find that specific instance in a list at a later point.

Currently I have something along the lines of this code:

static List<ClassItem> classList = new List<ClassItem>();

private void updateClassList(Stream stream)
        {
            Message = Recieve(stream);
            Interact(Message); //get the number of Classes about to be recieved

            string ID;
            string state;

            for(int i = 0; i < numberOfClasses; i++)
            {
                Message = Recieve(stream);
                interpretClassProperties(Message, out ID, out State);

                ClassItem ID = new ClassItem(ID, state); //incorrect code
                classList.Add(ID); //add new instance to list
            }
        }

Obviously this isn't going to work as I can't use a variable to initialise a class instance, however logically it shows what I want to achieve. Each loop will add an instance of ClassItem (with the appropriate ID value as a name) to the classList so I can find it later.

What should I look into in order to achieve this?

Any feedback appreciated, including any warnings of future problems I may have in approaching the problem in this fashion. (I.e. finding a class instance in a List by name).





Aucun commentaire:

Enregistrer un commentaire