473,473 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Jun 27 '08 #1
4 2123

OOps! Of course, the public keywords (before enum fields) must be ignored.
Jun 27 '08 #2
On 4 May, 19:34, LPeter <LPe...@discussions.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 ?
Jun 27 '08 #3


"Stan" wrote:
On 4 May, 19:34, LPeter <LPe...@discussions.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
Jun 27 '08 #4
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 "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)

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Russell Shaw | last post by:
Hi, gcc-3.4 complains about non-integers in: enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"}; even if i cast the strings to integers.
5
by: Sriram Rajagopalan | last post by:
Hi, Is the extra comma at the end of an enumerator-list valid according to the C standards? With the gcc compiler the following is valid: enum DAYS {MONDAY, TUESDAY, }day1; gcc does not...
10
by: James Brown | last post by:
I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING }; (it goes on and on like that) This is what I would like to do: TOKEN t1 = TOK_ID; // ok...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
12
by: Laurent Deniau | last post by:
If I understand well, an enumeration is only garantee to hold at most an int (6.7.2.2-2). So I would like to know: how to store a long in an enum? enum { p2_31 = 1L << 31 }; // boom how to...
9
by: sarathy | last post by:
Hi, Can anyone just help me with what exactly is constant expression. I read the section on Constant expression in K&R2. i am not fully clear with it.
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.