473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 4207
<ez*******@gmx.dewrote in message
news:11**********************@i39g2000hsf.googlegr oups.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.RuntimeEnvironment. GetSystemVersion();

Davey,

=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Beer is part of the life.
http://www.lovebeers.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
ezmera...@gmx.de 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*******@yahoo.yabbadabbadoo.comwrote in message
news:2F**********************************@microsof t.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;TUESDAY
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 programmatically, see
DefineConstants.

Example

Copy Code
// preprocessor_define.cs
// compile with: /define:xx
// or uncomment the next line
// #define xx
using System;
public class Test
{
public static void Main()
{
#if (xx)
Console.WriteLine("xx defined");
#else
Console.WriteLine("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(HtmlTextWriter writer)
P{
Pbase.Visible = true;
Pbase.RenderControl(writer);
P}
P#endif
P-Peter
P>
P"ez*******@gmx.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
automatically 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
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...
5
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...
1
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
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...
2
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...
4
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....
10
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...
1
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
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...
1
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.