Alternate enum value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kalvin.tuel@gmail.com

    #1

    Alternate enum value

    I have an enum marked with the flags attribute. I would like to
    assign an alternate value to each enum member so that instead of being
    stuck with the value needed for the flag I could give each member it's
    position in the enum. This is just an example, what I really need is
    to assign each value an ID from a database. Is there a builtin
    attribute for this or do I need to create a custom attribute to handle
    something like this?

    What I would like:
    [SYS.Flags]
    private enum enumTest
    {
    [RealValue(1)]
    First = 1,
    [RealValue(2)]
    Second = 2,
    [RealValue(3)]
    Third = 4
    };

    Thank you.
  • Mattias Sjögren

    #2
    Re: Alternate enum value

    >Is there a builtin
    >attribute for this or do I need to create a custom attribute to handle
    >something like this?
    No built-in attribute that I know of.

    >[SYS.Flags]
    >private enum enumTest
    >{
    [RealValue(1)]
    First = 1,
    [RealValue(2)]
    Second = 2,
    [RealValue(3)]
    Third = 4
    >};
    If all values follow the same pattern, you can calculate the "real
    value" as

    realValue = Math.Log((int)y ourEnumTestValu e, 2) + 1;


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • kalvin.tuel@gmail.com

      #3
      Re: Alternate enum value

      On Nov 15, 1:20 pm, Mattias Sjögren <mattias.dont.w ant.s...@mvps.o rg>
      wrote:
      Is there a builtin
      attribute for this or do I need to create a custom attribute to handle
      something like this?
      >
      No built-in attribute that I know of.
      >
      [SYS.Flags]
      private enum enumTest
      {
      [RealValue(1)]
      First = 1,
      [RealValue(2)]
      Second = 2,
      [RealValue(3)]
      Third = 4
      };
      >
      If all values follow the same pattern, you can calculate the "real
      value" as
      >
      realValue = Math.Log((int)y ourEnumTestValu e, 2) + 1;
      >
      Mattias
      >
      --
      Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.ne t/dotnet/|http://www.dotnetinterop.com
      Please reply only to the newsgroup.
      Thanks a lot. I'll give that a try.

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: Alternate enum value


        "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rgwrote in message
        news:Om5VZx7JIH A.2268@TK2MSFTN GP02.phx.gbl...
        >
        >>Is there a builtin
        >>attribute for this or do I need to create a custom attribute to handle
        >>something like this?
        >
        No built-in attribute that I know of.
        >
        >
        >>[SYS.Flags]
        >>private enum enumTest
        >>{
        > [RealValue(1)]
        > First = 1,
        > [RealValue(2)]
        > Second = 2,
        > [RealValue(3)]
        > Third = 4
        >>};
        >
        If all values follow the same pattern, you can calculate the "real
        value" as
        >
        realValue = Math.Log((int)y ourEnumTestValu e, 2) + 1;
        A lookup table should be much faster and not use that much memory, plus
        there needn't be any pattern.
        >
        >
        Mattias
        >
        --
        Mattias Sjögren [C# MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        Working...