Expression in enum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TFBldGVy?=

    Expression in enum


    Hi,


    I often wrote my constants in C/C++ in the following form:

    #define FOURCC(c0, c1, c2, c3) ( ((DWORD)(c0)) | ((DWORD)(c1) << 8) |
    ((DWORD)(c2) << 16) | ((DWORD)(c3) << 24) )

    #define CONST_A FOURCC('A','B', 'C','D') // A-tag: "ABCD"
    #define CONST_B FOURCC('E','F', 'G','H') // B-tag: "EFGH"
    #define CONST_C FOURCC('I','J', 'K','L') // C-tag: "IJKL"



    How can I write constant (I would prefer enum) in C# tastefully?
    I tried the enums and constant variables of a class but either accept direct
    expressions only:

    public enum MyConstants
    {
    public None = ('\0' | ('\0' << 8) | ('\0' << 16) | ('\0' << 24)),

    public A = ('A' | ('B' << 8) | ('C' << 16) | ('D' << 24)),
    public B = ('E' | ('F' << 8) | ('G' << 16) | ('H' << 24)),
    public C = ('I' | ('J' << 8) | ('K' << 16) | ('L' << 24))
    }


    It would be nice if I used the following form (or something similar):

    public enum MyConstants
    {
    public None = FOURCC('\0','\0 ','\0','\0'),

    public A = FOURCC('A','B', 'C','D'),
    public B = FOURCC('E','F', 'G','H'),
    public C = FOURCC('I','J', 'K','L')
    }


    Thanks for any good suggestion.


    LPeter



  • =?Utf-8?B?TFBldGVy?=

    #2
    RE: Expression in enum


    OOps! Of course, the public keywords (before enum fields) must be ignored.


    Comment

    • Stan

      #3
      Re: Expression in enum

      On 4 May, 19:34, LPeter <LPe...@discuss ions.microsoft. comwrote:
      Hi,
      >
      I often wrote my constants in C/C++ in the following form:
      >
      #define FOURCC(c0, c1, c2, c3) ( ((DWORD)(c0)) | ((DWORD)(c1) << 8) |
      ((DWORD)(c2) << 16) | ((DWORD)(c3) << 24) )
      >
      #define CONST_A FOURCC('A','B', 'C','D') // A-tag: "ABCD"
      #define CONST_B FOURCC('E','F', 'G','H') // B-tag: "EFGH"
      #define CONST_C FOURCC('I','J', 'K','L') // C-tag: "IJKL"
      >
      How can I write constant (I would prefer enum) in C# tastefully?
      I tried the enums and constant variables of a class but either accept direct
      expressions only:
      >
      public enum MyConstants
      {
              public None = ('\0' | ('\0' << 8) | ('\0' << 16) | ('\0'<< 24)),
      >
              public A = ('A' | ('B' << 8) | ('C' << 16) | ('D' << 24)),
              public B = ('E' | ('F' << 8) | ('G' << 16) | ('H' << 24)),
              public C = ('I' | ('J' << 8) | ('K' << 16) | ('L' << 24))
      >
      }
      >
      It would be nice if I used the following form (or something similar):
      >
      public enum MyConstants
      {
              public None = FOURCC('\0','\0 ','\0','\0'),
      >
              public A = FOURCC('A','B', 'C','D'),
              public B = FOURCC('E','F', 'G','H'),
              public C = FOURCC('I','J', 'K','L')
      >
      }
      >
      Thanks for any good suggestion.
      >
      LPeter
      Shouldn't you be using struct rather than enum ?

      Comment

      • =?Utf-8?B?TFBldGVy?=

        #4
        Re: Expression in enum



        "Stan" wrote:
        On 4 May, 19:34, LPeter <LPe...@discuss ions.microsoft. comwrote:
        Hi,

        I often wrote my constants in C/C++ in the following form:

        #define FOURCC(c0, c1, c2, c3) ( ((DWORD)(c0)) | ((DWORD)(c1) << 8) |
        ((DWORD)(c2) << 16) | ((DWORD)(c3) << 24) )

        #define CONST_A FOURCC('A','B', 'C','D') // A-tag: "ABCD"
        #define CONST_B FOURCC('E','F', 'G','H') // B-tag: "EFGH"
        #define CONST_C FOURCC('I','J', 'K','L') // C-tag: "IJKL"

        How can I write constant (I would prefer enum) in C# tastefully?
        I tried the enums and constant variables of a class but either accept direct
        expressions only:

        public enum MyConstants
        {
        public None = ('\0' | ('\0' << 8) | ('\0' << 16) | ('\0' << 24)),

        public A = ('A' | ('B' << 8) | ('C' << 16) | ('D' << 24)),
        public B = ('E' | ('F' << 8) | ('G' << 16) | ('H' << 24)),
        public C = ('I' | ('J' << 8) | ('K' << 16) | ('L' << 24))

        }

        It would be nice if I used the following form (or something similar):

        public enum MyConstants
        {
        public None = FOURCC('\0','\0 ','\0','\0'),

        public A = FOURCC('A','B', 'C','D'),
        public B = FOURCC('E','F', 'G','H'),
        public C = FOURCC('I','J', 'K','L')

        }

        Thanks for any good suggestion.

        LPeter
        >
        Shouldn't you be using struct rather than enum ?
        >
        What are you thinking for?
        LPeter


        Comment

        • =?UTF-8?B?TGFzc2UgVsOlZ3PDpnRoZXIgS2FybHNlbg==?=

          #5
          Re: Expression in enum

          LPeter wrote:
          Hi,
          >
          >
          I often wrote my constants in C/C++ in the following form:
          >
          #define FOURCC(c0, c1, c2, c3) ( ((DWORD)(c0)) | ((DWORD)(c1) << 8) |
          ((DWORD)(c2) << 16) | ((DWORD)(c3) << 24) )
          >
          #define CONST_A FOURCC('A','B', 'C','D') // A-tag: "ABCD"
          #define CONST_B FOURCC('E','F', 'G','H') // B-tag: "EFGH"
          #define CONST_C FOURCC('I','J', 'K','L') // C-tag: "IJKL"
          >
          >
          >
          How can I write constant (I would prefer enum) in C# tastefully?
          I tried the enums and constant variables of a class but either accept direct
          expressions only:
          >
          public enum MyConstants
          {
          public None = ('\0' | ('\0' << 8) | ('\0' << 16) | ('\0' << 24)),
          >
          public A = ('A' | ('B' << 8) | ('C' << 16) | ('D' << 24)),
          public B = ('E' | ('F' << 8) | ('G' << 16) | ('H' << 24)),
          public C = ('I' | ('J' << 8) | ('K' << 16) | ('L' << 24))
          }
          >
          >
          It would be nice if I used the following form (or something similar):
          >
          public enum MyConstants
          {
          public None = FOURCC('\0','\0 ','\0','\0'),
          >
          public A = FOURCC('A','B', 'C','D'),
          public B = FOURCC('E','F', 'G','H'),
          public C = FOURCC('I','J', 'K','L')
          }
          >
          >
          Thanks for any good suggestion.
          >
          >
          LPeter
          >
          >
          >
          The only way to get "expression s in enum" is to use some kind of code
          generator or pre-processor, neither of which is built into C#.

          I'd say you'll just have to hardcode the values.

          public enum MyConstants
          {
          None = 0x00000000,
          A = 0x40414243,
          B = 0x44454647
          }

          (I don't know how FOURCC combines the values so the above example values
          are probably wrong, you'll need to figure out the right values yourself)

          --
          Lasse Vågsæther Karlsen
          mailto:lasse@vk arlsen.no
          Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

          PGP KeyID: 0xBCDEA2E3

          Comment

          Working...