Conditional compilation question

D

Dave

I'm a C++ programmer of many years, trying to get my feet wet in C#.

I have a question about conditional compilation. In C++, I would
sometimes define a constant in an include file, and then have blocks of
code in different source files that were conditionally compiled based
on that constant. Now that C# has done away with include files, is
there any way of doing the same thing, short of defining the constant
multiple times at the head of each source file that has the
conditionally compiled code? There must be a better way...

Thanks.

- Dave
 
A

Andy

Dave,

There are conditional complier statements, and its done the same way
you would do it in C++, via the #if #endif statements.

In VS2005, open project properties and goto the Build tab; theres a
text box to define if the constants exist or not.

Be careful though, it is harder to test an assembly which uses
conditional compilation (as I'm sure you know).

Andy
 
S

Stoitcho Goutsev \(100\)

Dave,

C# supports conditional compilation, but it is more simple that the one in
C++
1. As you noted already there is no header files and because of this you
have to either define the preprocessor symbol in each file or you can define
it gloabally for the project in the project settings.

2. You can only define a symbol (giving it a name) unlike C++ you cannot
give it a value and then comapare against it value.

For example you can

#define VS_2005
....

#if VS_2005
....
#else
....
#endif

You can check only if some name is declared. Preprocessor declarations of
version numbers and checking with < , > or = are not supported.
 
D

Dave

Thanks, all.

Yes, I already know all this, but it still seems like C# is lacking in
this regard relative to C++. In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's. There isn't a way to have a constant defined for
the entire "solution", is there? In C++, this is easily done by having
a #define in an include file that all the dll and library modules in
the project include. This is done all the time...

- Dave
 
B

Brian Richards

If you're using msbuild you could do:

msbuild /property:DefineConstants=TRACE;DEBUG;Custom. MySolution.sln

-Brian
 
S

Stoitcho Goutsev \(100\)

Dave,
In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's.

This is not even possible in C++. These are pre-processor directives as such
they are taken into consideration before compilaton. If you reference a dll
this dll is already compiled, so all directives has been already processed
the sam if you implicitly link to a dll in C++. There is no #define in
already compiled code.
 
J

james.curran

Note that, as dictated by the standard, constant if() expressions are
evaluated at compile time, So, you can do, in C# code, what you were
doing with the preprocessor:

class Config
{
public const bool EnableThis = false;
}


if (Config.EnableThis)
{
/// blah-blah-blah
}
else
{
/// otherwise
}

Only the "otherwise" code will be compiled into the assembly.
 
D

Dave

Thanks, all.

Yes, I already know all this, but it still seems like C# is lacking in
this regard relative to C++. In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's. There isn't a way to have a constant defined for
the entire "solution", is there? In C++, this is easily done by having
a #define in an include file that all the dll and library modules in
the project include. This is done all the time...

- Dave
 
D

Dave

Stoitcho said:
Dave,


This is not even possible in C++. These are pre-processor directives as such
they are taken into consideration before compilaton. If you reference a dll
this dll is already compiled, so all directives has been already processed
the sam if you implicitly link to a dll in C++. There is no #define in
already compiled code.

Sure it's possible. In C++ developer studio you can have a
"workspace", analogous to the "solution" in Visual Studio .NET. The
workspace can include multiple dll's and executables. If the source
code in any dll or exe includes an include file that you change the
value of a constant in, the dll or exe will be rebuilt. People do this
~all~ the time, in any large project.

I think the solution is to do what James suggested. I think this is a
bit more of a hassle than what you do in C++, but it works.

Thanks everyone!

- Dave
 
S

Stoitcho Goutsev \(100\)

I think you are mixing referencing dlls vs referencing projects. DLLs are
already compiled assemblies and cannot be modified.

Anyways I'm glad you find a solution to your problem.
 
D

Dave

I'm not confused, but may be confusing. I'm talking about having an
exe with a project - call it "Executable.exe". That exe uses code from
a dll, call it "DLL.dll". You have a solution that contains the
projects for both Executable, and DLL. I wanted a way to have a
conditional compilation directive I could change from within that
solution, that would rebuild any code in the Executable or DLL, that
referenced the conditional compilation directive.
 

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

Top