none
Pre-defined Constants, preprocessor directives, etc...

    Question

  • Where can I find a list of all the built-in pre-defined constants.  When developing code and distributing it, it is appropriate to generate reusable code, however, not everyone uses the same compiler.  Some use VS 2005, some are using 2008, others are using Express editions, others still may be using #Develop open source IDE.  I'm looking to find the predefined constants that should exist that would allow me to taylor some code sections to the specific versions.

    There should be a preprocessor define that lets the programmer know the version of the compiler, like
    #if VS2005

    #elif VS2008

    #endif

    or even better would be one to determine the Framework the code is being compiled under.  As much as the .Net framework is very helpful, there are times when I just want to reuse code, not link to a previously generated assembly.  thus, i could write some code to work for 2.0, 3.0, 3.5, etc so when that one file that I link to in 12 different projects is compiled, it is compiled according to the framework of the project, etc. 

    many IDE's have these built in pre-processor defines that are automatic so code can be written more distributable.  Where can I find the list of such items for VC/VB.

    Thanks.

    Jaeden "Sifo Dyas" al'Raec Ruiner




    Wednesday, February 25, 2009 2:38 PM

All replies

  • As far as I know, C# does not have these pre-defined macros (like in C++).

    Also see http://msdn.microsoft.com/en-us/library/ed8yd1ha(vs.71).aspx and scroll down to the bottom, there's a line that says "Unlike C and C++ directives, you cannot use these directives to create macros."

    http://blog.voidnish.com
    • Proposed as answer by Harry Zhu Friday, February 27, 2009 1:28 PM
    Wednesday, February 25, 2009 3:08 PM
    Moderator
  • But these aren't macros, their constants.  

     macro being:

     #define sqr(x) (x * x)

    performs a function like text replacement.

    I'm asking for Compiler Version constants.  Like for say, Delphi (i know it isn't microsoft) but there is delphi4,dephi6, delphi2007, delphi2009, there is are constants that are predefined in the compiler itself so code can be written:

     #if VER>DELPHI6

     #elseif VER=DELPHI4

    #elseif VER=DELPHI6

    #endif

     Allowing the Code to be written for all versions of the delphi compiler no matter what the programmer is using to compile the code.

     A good use of this is the recently affected IEnumerable.Cast<> method which, altered how it ran in the back end from version 3.5 to 3.51 

     

    This would be good to notice in the compilation of an application, because if the Cast<> method is being used inappropriately, ie: int[] x={1,2,3}; x.Cast<String> <---ERROR!  the compiler could be written to detect:

    string[] s;

     #if NETVER=3.5

    s = x.Cast<string>();

    #elif NETVER=3.51

    Array.Resize(ref s, x.Length);

     for(int i=0;i<x.Length;i++) {

        s[i] = x[i].ToString();

    }

     #endif

     Though my syntax is probably not 100%, the idea is sound.

    Jaeden "Sifo Dyas" al'Raec Ruiner

     

    Friday, February 27, 2009 6:28 PM
  • NOT recommended.

    Take a peek inside of a VS2008 csproj file.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 27, 2009 8:21 PM
    Moderator
  • Rudedog2 said:

    NOT recommended.

    So?  I'm never interested in what people "Recommend" for a programming language.  I'm only interested in what it can do.  I take everything in these systems to the extreme, because far be it for me to judge, but most "recommendations" are there to prevent idiots from making mistakes. 
    I am not the former, because of the latter.  In order to learn, one must make mistakes, and frankly, i never take a "computer's" word on it.  I try it and see what it can do. 

    "Take a peek inside of a VS2008 csproj file."

    I do this quite frequently, and I edit them too, because sometimes I like to create linked files:

    EG:

        <Compile Include="..\..\Includes\GlobalExtensions.cs">
          <Link>Includes\GlobalExtensions.cs</Link>
          <Visible>true</Visible>
        </Compile>

    which thus far has been very difficult to achieve, if not impossible, from the IDE.  There are several source code files I use again, and again, and again, adn sometimes a new problem, or unexpected use comes into play effecting a modification to the file, adding overloads, etc.  I do not wish to rewrite the file 27 times for each program that uses it.  And for those that say use a class assembly, I do that as well, for larger applications.  but since the Signing of an application is such a royal pain unless you are willing to PAY for a signing service, it is very difficult and annoying to create internal company applications that may/may not install themselves into the GAC, as well, I'd rather not deal with extraneous DLL's lying about when I don't need them, so I compile the source directly. 

    It's like taking the .Net power, but not dealing with some of its annoyances. May not be what is "Recommended" but it is what Works. :)

    Cheers
    Jaeden "Sifo Dyas" al'Raec Ruiner

    Monday, March 23, 2009 3:35 PM
  • The predefined conditional compilation symbols are:

    DEBUG
    TRACE

    and... that's it.

    Monday, March 23, 2009 3:58 PM
  • 1. "NOT recommended."  That caution was intended not just for you.  There are many, many others who read these forums for ideas.  Some of them should have no business sniffing around inside of that type of file, much less modifying it.

    2.  The "C# Language Designers" decided to not include constants in the fashion in which C/C++ allows them.  To allow some, or even one, would open a Pandora's Box of voices suggesting new constants to be added.  Worser still, hordes of voices complaining about why they cannot define their own constants.

    3.  Visual Studio allows mixed solutions.  Why not create a mixed language solution that used C++ to selectively compile or include the stuff that you needed?  The core of the project(s) could still be written in C#.

    Hope this helps.

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."


    EDIT:  And as for your Delphi example.  I have written code for an API that gets updated every 13.333 months, requiring the developer to put create different versions of applications that target the different versions of the API. 

    Personally, I found it a real pain to have to re-write EVERYTHING every year because the wingnuts cannot decide on a definitive API.  It was a waste of my time.  Past tense.  I no longer use or target that API.
    Monday, March 23, 2009 4:13 PM
    Moderator
  • Rudedog2 said:

    1. "NOT recommended."  That caution was intended not just for you.  There are many, many others who read these forums for ideas.  Some of them should have no business sniffing around inside of that type of file, much less modifying it.

    That's fair.  Didn't think to much about other coders...but then again i've never been one for coddling.  But it makes sense to put an advisory: mess with this level of code at your own risk, etc.  :)

    2.  The "C# Language Designers" decided to not include constants in the fashion in which C/C++ allows them.  To allow some, or even one, would open a Pandora's Box of voices suggesting new constants to be added.  Worser still, hordes of voices complaining about why they cannot define their own constants.

    Well, we can define our own constants.  I mean, the DEBUG, and TRACE constants exist, and in the App properties we can add more, that are global constants, which I do use for many things.  RIght now i'm writing Generators and updating some system level stuff that make my life easier, and sometimes it is helpful to have easier access to conditional defines.  both vb and c# support conditional defines, but perhaps they were designed for different things than what C++/Delphi originally used them for.

    EDIT:  And as for your Delphi example.  I have written code for an API that gets updated every 13.333 months, requiring the developer to put create different versions of applications that target the different versions of the API. 

    Personally, I found it a real pain to have to re-write EVERYTHING every year because the wingnuts cannot decide on a definitive API.  It was a waste of my time.  Past tense.  I no longer use or target that API.

    Oh, I quite agree on that one, however, as a tool and library developer, especialy with regards to my previous example of the Cast<> Extension, the same thing still ends up happening with the .Net framework the same as it does with other compilers.  The .net API changes and thus some functionality changes, as in 3.5 I could to int[] x; x.Cast<string>() and it would "convert" the ints to strings.  with 3.51 it does a "cast" operation and did not leave any convert functionality for backwards compatibility.  I had to go through and modify all my code that used Cast<> in the convert manner just to get it to run.  In such a circumstance I would have simple made a conditionally compiled extension based upon the current framework that would compensate for this once, and then never have to modify other code that was written for 3.5, at recompile time it would just sub in the appropriate code.  Thus, it would use my less than optimal code to perform the convert if we're on 3.51 but for 3.5 it would use the .net frameworks "cast" to perform the operation.  I'm currently having a similar problem with Delphi again because their originally implementation had the String type always correlate to the AnsiString type, but with the 2009 compiler they've updated it so that the String type is now suddenly always a UnicodeString type which is not directly compatible and must be explicitly typecast. 

    Since I am using generators if you look in the WinApplication Template for either vb or cs, there are these conditionals in there:

    $if$ ($targetframeworkversion$ == 3.5)

    $endif$

    which are interpreted at creation of the project to include certain functionality.  I've modified some of this for my own project templates which helps some, and along with my custom codegenerators I've managed to reduce the amount of setup-time i have in creating projects that work the way i want them to. 

    So yea, i guess the overall lesson is that not everyone really should be diving this deep into the core framework of any language, and it is helpful to flag somethings as not recommended for the novice, meanwhile, for more advanced programmers we can create ways of writing code that is more universally compatible, but it takes a bit more work.

    Cheers!
    Jaeden "Sifo Dyas" al'Raec Ruiner

    "Never Trust a computer. Your brain is smarter than any micro-chip."
    Monday, March 23, 2009 5:59 PM
  • There is a difference between defining constants are pre-processor directives, which is what I thought you were asking for.  And I noticed that you skipped this altogether.

    "3.  Visual Studio allows mixed solutions.  Why not create a mixed language solution that used C++ to selectively compile or include the stuff that you needed?  The core of the project(s) could still be written in C#."

    There is a difference between constants that affect compiler behavior and constants that are defined for use in the executing assembly, like PI = 3.14259.....

    Happy Coding.

    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, March 23, 2009 6:14 PM
    Moderator
  •  How to: Create Solution and Project Build Configurations 

    http://msdn.microsoft.com/en-us/library/80bh6a6h(VS.80).aspx

    Not sure if I did that right.  Should be the same link posted twice.

    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, March 23, 2009 8:21 PM
    Moderator
  • I do this quite frequently, and I edit them too, because sometimes I like to create linked files:

    EG:

        <Compile Include="..\..\Includes\GlobalExtensions.cs">
          <Link>Includes\GlobalExtensions.cs</Link>
          <Visible>true</Visible>
        </Compile>

    which thus far has been very difficult to achieve, if not impossible, from the IDE. 
    I know this is an old thread, but on the off chance that you come back by here, you should know that you can do this pretty easily from the IDE.  Right-click your project in the Solution Explorer, click Add > Existing Item, select the file.  Then, instead of just hitting the Add button, hit the little down arrow next to the Add button and choose Add as Link.
    Tuesday, May 12, 2009 6:28 PM
  • Wow.

    Thanks Iceman.  That didn't exist in the express version that I was aware of, that does make my life a lot easier.
    J"SD"a'RR
    "Never Trust a computer. Your brain is smarter than any micro-chip."
    Tuesday, May 12, 2009 6:47 PM