By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

Conditional compilation question

P: n/a
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

Jun 8 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

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


Jun 8 '06 #2

P: n/a
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.
--
HTH
Stoitcho Goutsev (100)
"Dave" <dp*@eliassen.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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

Jun 8 '06 #3

P: n/a
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

Jun 8 '06 #4

P: n/a
If you're using msbuild you could do:

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

-Brian

"Dave" <dp*@eliassen.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
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

Jun 8 '06 #5

P: n/a
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.
--

Stoitcho Goutsev (100)

"Dave" <dp*@eliassen.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com... 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

Jun 8 '06 #6

P: n/a
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.

Dave wrote:
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...


Jun 8 '06 #7

P: n/a
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

Jun 8 '06 #8

P: n/a

Stoitcho Goutsev (100) wrote:
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.
--

Stoitcho Goutsev (100)


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

Jun 8 '06 #9

P: n/a
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.
--

Stoitcho Goutsev (100)

"Dave" <dp*@eliassen.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Stoitcho Goutsev (100) wrote:
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.
--

Stoitcho Goutsev (100)


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

Jun 8 '06 #10

P: n/a
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.

Stoitcho Goutsev (100) wrote:
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.
--

Stoitcho Goutsev (100)

"Dave" <dp*@eliassen.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Stoitcho Goutsev (100) wrote:
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.
--

Stoitcho Goutsev (100)


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


Jun 8 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.