#if #endif in CSharp

  • Thread starter Thread starter ma
  • Start date Start date
M

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
 
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:

m> Hello,

m> I want to write something such as :



m> enum x{

m> x1=0,

m> x2,

m> x3,

m> x4,

m> xend};



m> #if xend != 6

m> #error size f enum is not correct

m> #endif





m> How can I do this in csharp?



m> Regards
 
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
 
Jon Skeet said:
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
 
ma said:
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.
 
ma said:
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
 

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

Back
Top