Expression in enum

L

LPeter

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
 
S

Stan

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 ?
 
L

Lasse Vågsæther Karlsen

LPeter said:
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 "expressions 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)
 

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