1

I have a comboBox at my Form. When i'm pressing it, list of the names of the classes popped up. And when i'm choosing some name of the class there, i want to create object of that class and then work with it. All my classes have a common parent, so i used this code to get all my sub classes:

var subclassTypes = Assembly
                    .GetAssembly(typeof(ParentClass))
                    .GetTypes()
                    .Where(t => t.IsSubclassOf(typeof(ParentClass)));

So after that i just added this to ComboBox, and its's work okay, it's showing all the classes i need in string. But how can i make an accordance between string name of the class and class itself? How can i store that accordance and how can i make it?

2
  • Type.Name, Type.FullName in order to get name of the type? Dictionary<string, Type> if you want to store the corrspondence? Commented Jan 9, 2020 at 7:35
  • please post the code where you populate the combobox. As I understood you want to create an object from the class name, is that correct? is that all? what do you exactly mean with "accordance"? Commented Jan 9, 2020 at 7:36

2 Answers 2

2

You could store the types directly in the combobox and use the SelectedItem property to retrieve them in the SelectedIndexChanged event:

List<Type> subclassTypes = Assembly
        .GetAssembly(typeof(ParentClass))
        .GetTypes()
        .Where(t => t.IsSubclassOf(typeof(ParentClass))).ToList();


comboBoxTypes.DataSource = subclassTypes;
comboBoxTypes.DisplayMember = "Name";

Using the Activator.CreateInstance method you can create an object of that type.

private void ComboBoxTypes_SelectedIndexChanged(object sender, EventArgs e)
{
    Type itemType = comboBoxTypes.SelectedItem as Type;

    ParentClass item = (ParentClass)Activator.CreateInstance(itemType);
}

Disclaimer: this solution is for a parameterless constructor!

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much! It works good with constructor without parameters! But what if i have to use constructor with a parameter? Exception popped up. How can i solve that problem?
And also in the last string we do like "ParentClass item = ..." But can we work directly with a child, but not with the parent? I mean like, can we instead of the last string write something like: "itemType item = ..." And work directly with a child and use methods of it?
@Svemir "can we instead of the last string write something like: "itemType item = ..." And work directly with a child" you are asking, can I use a certain type at compile time that is only known at run time. The answer is: no, you cannot use it directly. Please provide more information about the child classes. Do you have different methods or properties in the children that you want to use and that the ParentClass does not have?
@Svemir "But what if i have to use constructor with a parameter?" it depends, There is actually an overload of CreateInstance in which you can input parameters, but it depends strongly on the constructors of your child classes. Again, I would need more information. Is the constructor parameter set different in each child class? There is also a solution for that
2

You already have all the Types of your classes. Creating a new instance of one of them is then just something like (assuming comboboxSelection as the selected class name):

var classType = subclassTypes.First(t => t.Name == comboBoxSelection);
var classInstance = Activator.CreateInstance(classType);

Note that in the example classInstance is of type object. You can easily cast it to the common type though:

var classInstance = (ParentClass)Activator.CreateInstance(classType);

Note that for Activator.CreateInstance to work, the classes need to have a constructor without parameters.

2 Comments

Thank you very much! It works good with constructor without parameters! But what if i have to use constructor with a parameter? Exception popped up. How can i solve that problem?
Thank you very much too for the answer! I wish here could be possibility to have many right answers. Cause both answers good and right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.