473,656 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conditional Compilation dependent on .net-Version

Hello,

I have some code which shall be compiled in .NET v1.1 and v2.0 but
which
has to be different for the two .NET-Versions.

Therefore I need to use a construct like

#if NET_20
<2.0 specific code>
#else
<1.1 specific code>
#endif

which enables the correct code at COMPILE TIME (it is not possible
to decide at runtime for my purposes!).

The question is: is there a define like NET_20 which is set
automatically by
the Visual Studio? Do you see any other chances to handle such
problems?

Thanks!

Jan 10 '07 #1
3 4229
<ez*******@gmx. dewrote in message
news:11******** **************@ i39g2000hsf.goo glegroups.com.. .
Hello,

I have some code which shall be compiled in .NET v1.1 and v2.0 but
which
has to be different for the two .NET-Versions.

Therefore I need to use a construct like

#if NET_20
<2.0 specific code>
#else
<1.1 specific code>
#endif

which enables the correct code at COMPILE TIME (it is not possible
to decide at runtime for my purposes!).

The question is: is there a define like NET_20 which is set
automatically by
the Visual Studio? Do you see any other chances to handle such
problems?

Thanks!

No , you have to specify your own Conditional Compilation symbol in your project build
settings. You have different projects for 1.1 and 2.0 anyway.

Willy.

Jan 10 '07 #2
If you need to get the CLR version during run time, using:

System.Runtime. InteropServices .RuntimeEnviron ment.GetSystemV ersion();

Davey,

=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Beer is part of the life.
http://www.lovebeers.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
ezmera...@gmx.d e wrote:
Hello,

I have some code which shall be compiled in .NET v1.1 and v2.0 but
which
has to be different for the two .NET-Versions.

Therefore I need to use a construct like

#if NET_20
<2.0 specific code>
#else
<1.1 specific code>
#endif

which enables the correct code at COMPILE TIME (it is not possible
to decide at runtime for my purposes!).

The question is: is there a define like NET_20 which is set
automatically by
the Visual Studio? Do you see any other chances to handle such
problems?

Thanks!
Jan 10 '07 #3
"Peter Bromberg [C# MVP]" <pb*******@yaho o.yabbadabbadoo .comwrote in message
news:2F******** *************** ***********@mic rosoft.com...
Here is the complete text of the MSDN2 documentation on this:

/define (Preprocessor Definition) (C# Compiler Options)

The /define option defines name as a symbol in your program.
/define:name[;name2]
Arguments

name, name2
The name of one or more symbols that you want to define.

Remarks

The /define option has the same effect as using a #define preprocessor
directive in your source file.
A symbol remains defined until an #undef directive in the source file
removes the definition or the compiler reaches the end of the file.

You can use symbols created by this option with #if, #else, #elif, and
#endif to compile source files conditionally.

/d is the short form of /define.

You can define multiple symbols with /define by using a semicolon or comma
to separate symbol names. For example:

Copy Code
/define:DEBUG;TU ESDAY
The C# compiler itself defines no symbols or macros that you can use in your
source code; all symbol definitions must be user-defined.

Note
The C# #define does not allow a symbol to be given a value, as in languages
such as C++.
For example, #define cannot be used to create a macro or to define a
constant.
If you need to define a constant, use an enum variable. If you want to
create a C++ style macro,
consider alternatives such as generics. Since macros are notoriously
error-prone, C# disallows their use but provides safer alternatives.
To set this compiler option in the Visual Studio development environment
Open the project's Properties page. For more information, see How to: Set
Project Properties (C#, J#).

Click the Build property page.

Modify the Conditional Compilation Symbols property.

For information on how to set this compiler option programmaticall y, see
DefineConstants .

Example

Copy Code
// preprocessor_de fine.cs
// compile with: /define:xx
// or uncomment the next line
// #define xx
using System;
public class Test
{
public static void Main()
{
#if (xx)
Console.WriteLi ne("xx defined");
#else
Console.WriteLi ne("xx not defined");
#endif
}
}

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Michael Nemtsev" wrote:
>Hello Peter Bromberg [C# MVP],

V2? mb smth else? It doesnt work for me

Where have u found it?

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

PTry:
P>
P#if V2
Ppublic override void RenderControl(H tmlTextWriter writer)
P{
Pbase.Visibl e = true;
Pbase.RenderCo ntrol(writer);
P}
P#endif
P-Peter
P>
P"ez*******@gm x.de" wrote:
P>
>Hello,

I have some code which shall be compiled in .NET v1.1 and v2.0 but
which
has to be different for the two .NET-Versions.
Therefore I need to use a construct like

#if NET_20
<2.0 specific code>
#else
<1.1 specific code>
#endif
which enables the correct code at COMPILE TIME (it is not possible to
decide at runtime for my purposes!).

The question is: is there a define like NET_20 which is set
automaticall y by
the Visual Studio? Do you see any other chances to handle such
problems?
Thanks!



True, but IMO you were suggesting "V2" being a pre defined Compiler Symbol which it is not.

Willy.

Jan 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2459
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be difficult to understand. I've described it as GOTO's on steroids, and that's what it is!. One argument against abolishing it it that it is useful for conditional compilation when porting code, etc. Well, it seems to me C++ supports that...
5
1758
by: ashok.anbalan | last post by:
Hi, I am working with a VC++ based tool that does hardware reads & writes besides some other stuff. I want to conditionally compile a piece of code based on the value of a field in a register i.e. only if the bit in the register is set I want to go ahead & compile the piece of code else it is left untouched. Basically, this has to happen at the pre-processor stage.
1
16550
by: chris han | last post by:
Hi, all, I'm trying to use Conditional Compilation Statements in my code. using System; #define DEBUG public class MyClass { public static void Main() {
12
2485
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int main()
2
2584
by: FireStarter | last post by:
Guys, in the code that follows, why does the method F() still compile, even if DBG is undefined? Inside method G(), the code inside <#if DBG> does not compile (notice that I can write whatever I want in there, I will not receive a compilation error). I do get such an error in F() - because of the garbage I intentionally put there - but F() should not compile in the first place. Am I misusing this attribute?! #undef DBG
4
2795
by: Bob | last post by:
Hi, In VS2003 conditional compilation constants and their state could be defined at project level. I was using this to control what features where offered by various builds. i.e. Feature1=true,Feature2=false, ... VS2005 seems to either allow the existance of the constant or not. ie. You must omit a constant not declare it to be false This means that the list no longer declares the state of all the options.
10
3086
by: Dave | last post by:
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...
1
2295
by: Marek | last post by:
I use VS2005 with framework 2.0 and I just found a behavior I consider odd. Here is the code that illustrates th eproblem: public static void MethodA() { MethodB() } #if DEBUG
6
2847
by: maxwell | last post by:
I'm trying to use the gpp utility (Gnu points to http://en.nothingisreal.com/wiki/GPP) to do conditional compilation in Python, and I'm running into a problem: the same '#' character introduces Python comments and is used by default to introduce #ifdef etc. lines. Here's an example of what I'm trying to do: #ifdef DEBUG stderr.write("variable is...") #details of msg omitted #endif
1
2182
by: Sagaert Johan | last post by:
Hi Is there a constant i could use to force conditional compilation based on whether i compile for CF or the Full NET framework ? Johan
0
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.