|
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 | |
Share:
|
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 | | |
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 | | |
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 | | |
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 | | |
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 | | |
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... | | |
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 | | |
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 | | |
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 | | |
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
| | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
11 posts
views
Thread by Steven T. Hatton |
last post: by
|
2 posts
views
Thread by Steve Jorgensen |
last post: by
|
1 post
views
Thread by chris han |
last post: by
|
12 posts
views
Thread by wanghz |
last post: by
|
2 posts
views
Thread by FireStarter |
last post: by
|
1 post
views
Thread by A.M-SG |
last post: by
|
4 posts
views
Thread by Bob |
last post: by
|
1 post
views
Thread by Marek |
last post: by
|
6 posts
views
Thread by maxwell@ldc.upenn.edu |
last post: by
| | | | | | | | | | |