Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to define precompiler variable

Posted on 2007-04-07
Medium Priority
Last Modified: 2013-11-26
I'd like to set a precompiler variable in my project settings rather than a config file.  In the project properties, I've added this:

/d: MYVAR=true

In my code, I do this:

  //variable is true
  //variable is not true

I always hit the second conditional, where it is not true.  But I don't know if .NET even sees MYVAR.  How can I set the variable as a precompiler?
Question by:brettr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 64

Expert Comment

by:Fernando Soto
ID: 18870080
Hi brettr;

Not sure what you are trying to do but this is how the use a precompile variable

At the top of your source file place the variable you want to define like so:

#define MYVAR

Then when you want the compiler to use one set of statements depending on the use of that variable
you do the following.
        private void Form1_Load(object sender, EventArgs e)
            MessageBox.Show("My Var is defined");
            MessageBox.Show("My Var is NOT defined");

Because you have the #define MYVAR at the top of the source file the above will always print "My Var is defined".

If you do not have a statement at the top of the source file #define MYVAR or you have the statement #undef  MYVAR then the program will always display the message box with the message "My Var is NOT defined".

If this is what your question is about fine otherwise let me know.


Author Comment

ID: 18870121
Yes - that worked.

What than do the "Conditional Compilation Symbols" in the project properties Build tab do? That's where I was setting my variable previously but it was never recognized.
LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 18870208
Hi brettr;

I did not know that was there. It is another way to set the precompile variables. But it seems to have a bug or the documentation is wrong. So this is how it works. To define one or more variables place the /define or the shortcut /d at the start of the text box. The the documentation states to place a : next but that seems to stop the first variable from being defined. So for example if you had the following in the "Conditional Compilation Symbols" text box /define:MYVAR;MYVAR2 the MYVAR would not be defined but MYVAR2 would be defined. If you leave out the : then both work OK. /Define MYVAR;MYVAR2 will work fine for both.

So in your case this will work:
/define MYVAR

But the following will not work

That's Microsoft for you. ;=)


Author Comment

ID: 18870226
Very cool.  Thanks on the trial and error to find that out.

"That's Microsoft for you. ;=)"

Who else would it be  :o
LVL 64

Expert Comment

by:Fernando Soto
ID: 18870233
Not a problem; glad to help. ;=)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question