Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Maybe I just don't know how to ask google the right way but I cannot for the life of me find the answer or way to do this:

I have written a base control for objects I intend to use in the Windows Forms Designer, declared as abstract, where within it, I have a bunch of properties that are not abstract.

One of them has a TypeConverter attribute applied to it with my converter. This is a converter for an enum where I break the enum's strings into space delimited phrases.

This enum has values that are valid for some of the derived classes and not for others. Is there a way I can have this be a base class and override the pieces I need for the child controls?

The reason is, all the code for the proprety is the same for all the children and I would hate to copy it from the parent to all the children just to change the TypeConverter attribute.

Here is a code example of what I am looking to do:

C#
public enum CustomEnum
{
    ValueNumber1,
    ValueNumber2,
}

public class CustomEnumConverter : TypeConverter
{
    public CustomEnumConverter()
    {
        supportedEnums = new ArrayList(Enum.GetValues(typeof(CustomEnum))
            .Cast<CustomEnum>().ToList());
    }

    (other code/functions implementing converter)

    private ArrayList supportedEnums;
}

// This will be used to remove "unsupported" values from the 
// supportedEnums ArrayList in the parent.
public class CustomClassBEnumConverter : CustomEnumConverter
{
}

public abstract class A : UserControl
{
    [Bindable(true)]
    // This can be different based on what child it is....
    [TypeConverter(typeof(CustomEnumConverter))]
    public CustomEnum MyBaseClassEnum
    {
        get { (code to return the value) };
        set { (code to set the value) };
    }
}

// So when an instance of this is dropped on a design surface, it will have 
// MyBaseClassEnum in the Forms Designer, but it may not have all the 
// enums available in the designer, only some of them.
public class B : A
{
}
Posted

1 solution

Quote:
The reason is, all the code for the proprety is the same for all the children and I would hate to copy it from the parent to all the children just to change the TypeConverter attribute.

try to make this property virtual
C#
public abstract class A : UserControl
{
    [Bindable(true)]
    // This can be different based on what child it is....
    [TypeConverter(typeof(CustomEnumConverter))]
    public virtual CustomEnum MyBaseClassEnum
    {
        get { (code to return the value) }
        set { (code to set the value) }
    }
}

// So when an instance of this is dropped on a design surface, it will have
// MyBaseClassEnum in the Forms Designer, but it may not have all the
// enums available in the designer, only some of them.
public class B : A
{
    // This can be different based on what child it is....
    [TypeConverter(typeof(CustomClassBEnumConverter))]
    public override CustomEnum MyBaseClassEnum
    {
        get { return base.MyBaseClassEnum; }
        set { base.MyBaseClassEnum = value; }
    }
}
 
Share this answer
 
Comments
David Knechtges 3-Apr-14 9:58am    
I ended up doing exactly what you suggested before I saw your response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900