473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

#if #endif in CSharp

ma
Hello,

I want to write something such as :

enum x{

x1=0,

x2,

x3,

x4,

xend};

#if xend != 6

#error size f enum is not correct

#endif

How can I do this in csharp?

Regards


Oct 15 '07 #1
5 2394
Hello, ma!

In terms of pre processor directives you can do.

enum x{
x1=0,
x2,
x3,
x4,

#if !xend_define
xend,
#endif
};
if you have to check xend for equality with 6, you can do this in the
runtime.

if ( x.xend != 6 )
{
//do something
}
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Mon, 15 Oct 2007 12:00:09 +0100:

mHello,

m I want to write something such as :

menum x{

mx1=0,

mx2,

mx3,

mx4,

mxend};

m#if xend != 6

m #error size f enum is not correct

m#endif

mHow can I do this in csharp?

mRegards

Oct 15 '07 #2
On Oct 15, 12:00 pm, "ma" <m...@nowhere.comwrote:
I want to write something such as :

enum x{
x1=0,
x2,
x3,
x4,
xend};

#if xend != 6
#error size f enum is not correct
#endif

How can I do this in csharp?
You can't - the "pre-processor" directives are much more limited.

However, you can easily write a unit test to accomplish a similar
goal.

Note that using Enum.GetValues you don't really need a "final element"
marker usually.

Jon

Oct 15 '07 #3
ma

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@e9g2000prf.googlegrou ps.com...
On Oct 15, 12:00 pm, "ma" <m...@nowhere.comwrote:
> I want to write something such as :

enum x{
x1=0,
x2,
x3,
x4,
xend};

#if xend != 6
#error size f enum is not correct
#endif

How can I do this in csharp?

You can't - the "pre-processor" directives are much more limited.

However, you can easily write a unit test to accomplish a similar
goal.

Note that using Enum.GetValues you don't really need a "final element"
marker usually.

Jon
Thanks.

How can report to user that the enum size is not correct during compile time
instead of generating an error during run time?

Regards
Oct 15 '07 #4
Lew
ma wrote:
How can report to user that the enum size is not correct during compile time
instead of generating an error during run time?
Write correct code. There is no "user" at compile time, only the programmer.

If you write wrong enum code, how can you be sure you'll get the length
parameter correct in the same code?

What if you refactor the code later? Doesn't that risk making the check
wrong? It's more maintenance work to update the enum, at the least.

The best way to be sure the enum is correct is to write and run unit tests -
yes, at run time, but before deployment time. Using the code to check itself
just puts you into an Escherian loop of verifying through the same code that
is verified.

--
Lew
Oct 15 '07 #5

"ma" <ma@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@e9g2000prf.googlegrou ps.com...
>On Oct 15, 12:00 pm, "ma" <m...@nowhere.comwrote:
>> I want to write something such as :

enum x{
x1=0,
x2,
x3,
x4,
xend};

#if xend != 6
#error size f enum is not correct
#endif

How can I do this in csharp?

You can't - the "pre-processor" directives are much more limited.

However, you can easily write a unit test to accomplish a similar
goal.

Note that using Enum.GetValues you don't really need a "final element"
marker usually.

Jon

Thanks.

How can report to user that the enum size is not correct during compile
time instead of generating an error during run time?

Regards

It is possible to write a custom FXCop rule to do this, although I have no
experience with this.

The specific problem you are trying to solve may have a solution other than
an enum. ( I'm guessing you have maitenance concerns ) You could try
using a static class with constants for members. Implement a method called
GetValues() for iteration. The client code would use the same syntax as if
it were an enumeration. Plus, the values would less likely to accidentally
change by another programmer's modification.
--Matt

Oct 15 '07 #6

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

Similar topics

0
by: Jonathan Grobe | last post by:
From: Nigel Perry <nigel@cosc.canterbury.ac.nz> Newsgroups: news.announce.newgroups,news.groups Subject: RFD: comp.std.csharp Date: Tue, 08 Jul 2003 00:26:01 +0000 REQUEST FOR DISCUSSION (RFD)...
6
by: boki | last post by:
Dear All, Could you please tell me what is a "#" stand for in c++? Why there is "#if 1" in program description? It go with "#endif" Could you please explain it? thank you very much.
1
by: Christopher M. Lusardi | last post by:
Hello, Is there anyway to do it other than using : #ifdef VAR1 ... #endif
7
by: Peter Smirnov | last post by:
Sorry for this newbie question but as far as I heard one need at least VisualStudio to develop CSharp applications. Is this correct? Are there otherwise some command line tools like javac.exe and...
2
by: news.microsoft.com | last post by:
Hi: I work in Csharp's parser files by LEX/YACC.Now I have only CSharp-lex.l and CSharp.y file,but they not have CSharp'comment Parse. this is a part of CSharp-lex.l. ........................
1
by: mohan | last post by:
Hi All, I have large file coded consisting of function implemented in c. This function consissts of #if macro and corresponding. And somewhere in the function i am missing to put #endif , Is...
0
by: Fabio R. | last post by:
I'm going crazy... Can someone help me to do a regex to search for something like nested if? I have these tags: <if>...<else>...<endif> that can be nested: <if> <if>...<else>...<endif>
2
by: Karl | last post by:
Hi all, I'm reasonably new to csharp so you have to forgive me asking what may be a stupid question... As I said, I'm new to CSharp but in VB I was able to click anywhere on a DataGridView...
14
by: lawjake | last post by:
I am having a dispute at work over endifs, and cannot find any treatise on this. So I am hoping that I can get a lot of correspondece confirming my belief. Here it is: I believe the following:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.