How to extend an enum

S

ssg31415926

I have an abstract base class with an enum which is passed into a
method. I want my derived classes to be able to add new values to the
enum without having to redefine it and I want to be able to pass them
into the method defined into the base class. Is there a way to do
this.

E.g.

abstract public class BaseWidget
{
public void Clear(Wodget wodget)
{
//do stuff with wodget
}

//other methods, etc.

public enum Wodget
{
wodget1, wodget2
}
}

public class LargeWidget: BaseWidget
{
//other methods, etc.

public enum Wodget
{
wodget3, wodget4
}
}

public class SmalllWidget: BaseWidget
{
//other methods, etc.

public enum Wodget
{
wodget5, wodget6
}
}
but I want the Wodget available to LargeWidget to contain wodgets 1, 2,
3 and 4 and the Wodget available to SmallWidget to contain wodgets 1,
2, 5 and 6.

I'd like this to be automatic so taht wodgets added to the base class
automatically appear in the derived class enums (otherwise I know that
someone's going to miss adding a new one to the derived class at some
point in the future.
 
J

Jon Skeet [C# MVP]

ssg31415926 said:
I have an abstract base class with an enum which is passed into a
method. I want my derived classes to be able to add new values to the
enum without having to redefine it and I want to be able to pass them
into the method defined into the base class. Is there a way to do
this.

I'm afraid you can't add anything to an enum. Could you give a bit more
detail about what you want to achieve here? There may well be a better
way - for instance, taking a variable number of enums, and when you
need to iterate, iterating through all of them.

Jon
 
S

ssg31415926

Thanks. I've realised I was being a bit dim. In my getter/setters, I
couldn't pass a null or String.Empty to a wrapped object's setter
because the wrapped object's method wouldn't accept it. Instead, I had
to call a Clear function and pass it a property name. For some reason,
it didn't occur to me to put this in the setter and I was off writing a
generic clear function and using the enum to hold the name of the
property to be cleared! The solution came to me on the way to lunch.
I think maybe I had one too many last night!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top