Solved

How to define precompiler variable

Posted on 2007-04-07
5
465 Views
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:

#if MYVAR
  //variable is true
#else
  //variable is not true
#endif

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?
0
Comment
Question by:brettr
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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)
        {
#if MYVAR
            MessageBox.Show("My Var is defined");
#else
            MessageBox.Show("My Var is NOT defined");
#endif
        }

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.

Fernando
0
 

Author Comment

by:brettr
Comment Utility
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.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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:
/d MYVAR
/define MYVAR

But the following will not work
/d:MYVAR
/define:MYVAR

That's Microsoft for you. ;=)

Fernando
0
 

Author Comment

by:brettr
Comment Utility
Very cool.  Thanks on the trial and error to find that out.

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

Who else would it be  :o
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem; glad to help. ;=)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Chat Room 1 22
Open form in the top right hand corner of screen 5 18
Long URL in SMS 6 20
Create XML 5 29
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now