Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Extending Enum in inherited class

Back to Forum: Tech Off
  • User profile image
    cheong

    Say that I have a "ClassB" inherited from "ClassA", and "ClassA" has a enum member called "Status":

    class ClassA
    {
        enum Status
        {
           Status1,
           Status2,
           Status3
        }
        // ... some other code
    }


    Now I want to extend the functionality of "ClassA" by being able to detect "Status4" and "Status5", so I'm going to define a new enum named "StatusEx". Do I have to create a new enum and copy every status in, or I can do something like this:

    class ClassB : ClassA
    {
        enum StatusEx : Status
        {
            Status4,
            Status5
        }
        // ... some other code
    }

    Note: I know the above code won't compile, just to demonstrate the idea of what do I want to achieve.

    Thanks for any input. Smiley

    P.S.: Actually I wish to have a enum type that can perform one-to-one matching, so I can directly assign a value of Status to StatusEx.

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    JohnAskew

    Consider using an Interface?
    Perhaps the template method pattern?

  • User profile image
    ScanIAm

    If you wanted, you could create a separate, static class to hold the statuses.  Then, when you started up, each class would feed it's own statuses into the static class where they'd be stored in a hashtable.

    If you need to do this programmatically, I can't figure out how the base class could understand a status it's never seen.

  • User profile image
    cheong

    JohnAskew wrote:
    Consider using an Interface?
    Perhaps the template method pattern?


    Unfortunately, the ClassA happens to be a native class of .NET framework so I have little control on that, the best I could hope for is to create a type that is compatible of ClassA.Status so I can directly assign the value of it to a variable of ClassB.StatusEx type.

    ScanIAm wrote:
    If you wanted, you could create a separate, static class to hold the statuses.  Then, when you started up, each class would feed it's own statuses into the static class where they'd be stored in a hashtable.

    If you need to do this programmatically, I can't figure out how the base class could understand a status it's never seen.

    No, I won't use StatusEx in any of the methods provided by ClassA.

    I just want to way to directly assign the value to the new variable type, instead of using "switch" statement to map it one by one.

    (In the analogy of that, assume ClassA.Status to be byte and ClassB.StatusEx to be integer. I wouldn't assign an integer to byte as overflow may occur. And it'd be quite insane to have huge "switch table" to map "1" to "1", then "2" to "2", etc. when doing conversion, right?)


    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    odujosh

    Enums are only good for list of key value pairs that do not change much if ever. .NET allows you to get away with this (below). If you have too many red flags going up for future matienance issues. You are better off using a Dictionarty Collection or implement some kind of string indexer. Because your design doesnt align very well with the design of enum types.

    class Class1

    {

    enum myEnum { one, two, three };

    }

    class Class2 : Class1

    {

    enum myEnum {one, two, three, four, five };

    void mymethod()

    {

    int x = myEnum.five;

    int x = myEnum.one;

    }

    }

  • User profile image
    marcusts

    I just posted a proposed solution.  Let me know what you think:

     

    http://www.codeproject.com/KB/cs/EnumBuilder.aspx

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.