0

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).

rashfmnb
  • 9,959
  • 4
  • 33
  • 44
James
  • 356
  • 2
  • 13
  • 1
    Possible duplicate of [c# instantiate class from string](http://stackoverflow.com/questions/2247598/c-sharp-instantiate-class-from-string) – Tyler Benzing Apr 15 '16 at 19:16
  • How will you need to "find that particular instance at a later time"? There are a few ways to do what you want, I would just use a `Dictionary` instead. When you want to get a certain ID, you just do `ClassItem thing = dictionary[ID]`, where `ID` is a string ID (or whatever you want, you can do `Dictionary`, etc..) – Quantic Apr 15 '16 at 19:20
  • @Quantic Using a dictionary instead of a list sounds like a potential fix. That way I can search through the dictionary for the relevant key and the value the instance of the class. In terms of my solution however, if I were to use a dictionary instead, would I run into problems if my classes were called the same thing. For example, its in a for loop and so each new instance would be called "thing" (to take your example). – James Apr 15 '16 at 19:27
  • You don't need a variable name. You can do this: `for (i=0;i – Quantic Apr 15 '16 at 19:37

2 Answers2

0

Use Activator.CreateInstance:

public static ObjectHandle CreateInstance(
    string assemblyName,
    string typeName
)

You know your assembly name and receive the class name (type name).

MSDN: https://msdn.microsoft.com/en-us/library/d133hta4(v=vs.110).aspx

Sample code:

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

private void updateClassList(Stream stream)

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

        string id;

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

            classList.Add(Activator.CreateInstance("AssemblyName", id).Unwrap());
        }
    }
Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
  • Looked into using this solution and it all seems to be correct and functions exactly how I outlined in the question. However, in response to a comment , in this case it seems I'm better of using a dictionary with the key as the name and the value as the class instance. Marking this as answered due to this answer doing exactly what I originally outlined. – James Apr 15 '16 at 19:30
0

Is something like this what you're looking for? Beware, though, this will likely create a memory nightmare in time unless you are certain to dereference your instances from the static list.

public class ClassWithIds
{
    public static List<ClassWithIds> Instances = new List<ClassWithIds>();

    private static int _idSeed = 0;

    private readonly string _name;

    public string Name
    {
        get
        {
            return _name;
        }
    }

    private static int NextId()
    {
        return Interlocked.Increment(ref _idSeed);
    }

    public ClassWithIds()
    {
        _name = this.GetType().FullName + " Number " + NextId();
        Instances.Add(this);
    }
}
David Morton
  • 16,338
  • 3
  • 63
  • 73