Preprocessor directives in codebehind file

T

Theon Greyjoy

Hi all,

I have a code behind file which I would like to conditionally compile
parts of.

The code is in C#. here is what I want to achieve, in pseudo C++ style:

--------------------------------
#define PRODUCTION 1

public partial class _MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#ifdef PRODUCTION
// My code
#endif
}

protected void button1_Click(object sender, EventArgs e)
{
#ifndef PRODUCTION
// More code
#endif
}

protected void button2_Click(object sender, EventArgs e)
{
#ifndef PRODUCTION
// More code again
#endif
}
}
-----------------------------------

In the above, you would think that the code in Page_Load would be
compiled, and the code in the button click events would not be.
Vice-versa if I removed the #define line.

However, I get the error "Cannot define/undefine preprocessor symbols
after first token in file" for the #define line. I've tried putting it
above and below the using statements, inside the class, with and without
a semi-colon, etc, but I can't get it to work. Is it because the class
is partial? Any other ideas?

Thanks in advance,
-Theon
 
B

bruce barker \(sqlwork.com\)

you have several syntax errors.

1) define is used to define a var, not set a value. if defined the value is
true, if not defined the value is false, so your code should be

#define PRODUCTION

2) thered is no #ifdef, just use #if

#if PRODUCTION
// this will only be included if PRODUCTION is defined
#endif

-- bruce (sqlwork.com)
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top