Don chan
Don chan

Reputation: 35

How to get value of the property of the child classes from the parent?

I have a base class named Template. And i have many children classes like TemplateChild1, TemplateChild2 etc that inherited Template class. All child classes basically have the same structure and looks like this:

public class TemplateChild1 : Template
{
    public const string Name = "Child class 1";
    //many other properties
}

public class TemplateChild2 : Template
{
    public const string Name = "Child class 2";
    //many other properties
}

//and many more other child classes with similar structure

In my task i have to show all this child classes of the Template class in the ComboBox on the form. So for this i used this code:

var templateTypes = Assembly
                    .GetAssembly(typeof(Template))
                    .GetTypes()
                    .Where(t => t.IsSubclassOf(typeof(Template))).ToList();

Then i put this list at the ComboBox and it works fine. But in ComboBox i see only the developing name of the class, but not the value of the Name property. For example, instead of "Child class 1"/"Child class 2"/etc. , i see TemplateChild1/TemplateChild2/etc. How can i fix that problem?

Upvotes: 0

Views: 1678

Answers (2)

Derrick Moeller
Derrick Moeller

Reputation: 4950

To elaborate on the other answer, the question originally made use of constants. You can't do this with a base class, but you can make your properties readonly this will at least prevent them from being written to outside of the base class constructor.

public class Template
{
    public string Name { get; }

    public Template(string name)
    {
        Name = name;
    }
}

public class TemplateChild1: Template
{
    public TemplateChild1()
        : base("Child one")
    { }
}

Upvotes: 1

Caius Jard
Caius Jard

Reputation: 74605

You've got your notions of inheritance a little upside-down. If both Child1 and Child2 have a "Name" property, you make the Name a property of the parent (all children then have a Name), and then you can treat all children as if they were an instance of their parent, and get the Name

List<Parent> x = new List<Parent>();

x.Add(new TemplateChild1());
x.Add(new TemplateChild2());

myCombo.DataSource = x;
myCombo.DisplayMember = "Name"; //tell databinding to use the Name property for the displaytext

For a class setup of:

public class Parent{
    public string Name { get; protected set; }
}

public class TemplateChild1: Parent
{
    public TemplateChild1(){
        Name = "Child one";
    }
}

public class TemplateChild2: Parent
{
    public TemplateChild2(){
        Name = "Child two";
    }
}

Now all the items in the combo, be they a Child1 or a Child2, show the text assigned to their Name property. The set is protected; it can only be accessed by children of the Parent. The constructors for the children set the value of Name, and it cannot then be set to something else (well.. except by another child. If you want to prevent that, make the children not inheritable), hence it's now constant

Upvotes: 4

Related Questions