Overriding Enum members

How can I override or overload Enum public methods e.g CompareTo or ToString


Answer this question

Overriding Enum members

  • Alex Norko

    I know that this sounds like wierd.... But this is what I am trying to accompolish...

    Take an e.g.
    Enum Numbers
    One=1
    Two=2
    Three=3
    End Enum

    What I was trying to do is to override the ToString or the GetName method to return a string other than the enumeration name.
    Like
    Dim a as Numbers=Numbers.One

    Now, I was trying to get something like "Oneself" from the overloaded version of GetName or ToString.

    I know there could be other methods of achieving the same effect (Collections, or arrays or even classes with Public constants), but I was hell bent on doing it with enums unless I realized that it couldn't be....


  • Misiacik7

    You can return the String name of the enumerated value with the code I gave above

  • AMSS

    What are you trying to accomplish...have you looked into the ENUM class:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemenumclasstopic.asp

    it will allow you to work with enumerations like so:

    [Enum].GetName(MyEnumeration, TheValue)



  • ferlinco

    There is a specific reason why I want to do it with enums...
    Enums make the process of defining a number of values form which a vraiable can assume one, a lot easier. In addition, the symbolic constants also increase clarity.

    Now, my problem is that I want symbolic constants with spaces in between them, like...
    Enum Sample
    Sample Text1=1
    Sample Text2=2
    Sample Text3=3
    End Enum

    So, now can someone suggest me a way to do it with enums. (Now, it should be pretty clear why I wanted to override ToString, to return strings with spaces)...

  • Matt24

    you cannot...

    To override a method you should have to inherit from a valuetype but this is prohibited


  • one_pom

    Nothing extraordinary, just trying out some fancy things..... (I was trying to find a way to assign strings to enum members rather than int)...

  • MosheDeutsch

    The quick answer is that you cannot override those members directly (in VB or IL). But there may be a different solution for what you're trying to achieve. Can you provide some examples of the behavior that you'd like to see

  • Overriding Enum members