PC Review


Reply
Thread Tools Rate Thread

Conditional Compilation dependent on .net-Version

 
 
ezmeralda@gmx.de
Guest
Posts: n/a
 
      10th Jan 2007
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!

 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      10th Jan 2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

 
Reply With Quote
 
Davey
Guest
Posts: n/a
 
      10th Jan 2007
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!


 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      10th Jan 2007
"Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
news:2F0B6E2D-373B-44A1-8F0B-(E-Mail Removed)...
> 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
> /defineEBUG;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
>>
>> P> Try:
>> P>
>> P> #if V2
>> P> public override void RenderControl(HtmlTextWriter writer)
>> P> {
>> P> base.Visible = true;
>> P> base.RenderControl(writer);
>> P> }
>> P> #endif
>> P> -Peter
>> P>
>> P> "(E-Mail Removed)" 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Conditional Compilation dependent on .net-Version Michael Nemtsev Microsoft C# .NET 0 10th Jan 2007 09:35 PM
Conditional compilation Constant for CLR version RC Microsoft C# .NET 1 14th Dec 2006 05:04 PM
Re: Conditional Compilation using Application.Version Stephen Bullen Microsoft Excel Programming 0 9th Nov 2004 08:48 AM
Re: Conditional Compilation using Application.Version Dave Peterson Microsoft Excel Programming 0 9th Nov 2004 01:13 AM
conditional compilation by framework version Petr Majer Microsoft Dot NET Framework 1 23rd Jul 2003 06:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:31 AM.