Sign in to follow this  
MTclip

c# and #define

Recommended Posts

MTclip    242
Is there any way c# to create 1 #define that can be seen by all files? .. for instatnce I have an application that runs on XP and CE but uses different code paths for each... but I have to put the #define at the top of each file... surely there is a way to set it up like in c++.. thanks

Share this post


Link to post
Share on other sites
TheUnbeliever    963
There may be some workaround, but as far as I can remember, #defines have file scope only in C#. So in short: no.

In any case, #defines are often bad things. Using const leads to all sorts of goodness (type correctness, and the impossibility of unrelated code being replaced by an errant preprocessor directive).

Your example could presumably be replaced by determining the OS type (at run or build time) in a function and returning the appropriate path?

Share this post


Link to post
Share on other sites
raz0r    586
In VS05 look at the Build sub-section of your project properties, there you will find "Conditional Compilation Symbols".

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You can use a class of all static members to do something like this and keep your constants organized in one file. I'm pretty sure the compiler is smart enough to not have you take a dereference penalty for this, but even if so you're dealing with managed code, and are going to take a hit anyways.


public class Constants
{
public static int CONSTANT_INT_1 = 5;
public static string CONSTANT_STRING_1 = "astring";
}

etc.


Then just use them like this:

speed += Constants.CONSTANT_INT_1;

Share this post


Link to post
Share on other sites
raz0r    586
Quote:
Original post by DaBookshah
C# has no preprocessor, and therefore no #define

There is support for conditional compilation - so yes #define.

Share this post


Link to post
Share on other sites
Spoonbender    1258
Depending on what you want to use it for, one or more of the above posts should cover it.

C# doesn't have a preprocessor, but it does have #define for conditional compilation. You can #define stuff either at file scope (with #define) or in the build options for the project.

And if you don't want conditional compilation, but rather just being able to define constants (C++-style), you can't. Have to use an enum or a bunch of static const variables.

Share this post


Link to post
Share on other sites
Zahlman    1682
Um, that thinking is a bit flawed. C++ and C don't really "have" a preprocessor either. The preprocessor is a separate entity, in effect; and nothing (except possibly lack of technical know-how) prevents you from running it on C# source, Java source, Python source, or your English essay for school.

Share this post


Link to post
Share on other sites
kanato    568
Quote:
Original post by MTclip
Is there any way c# to create 1 #define that can be seen by all files? .. for instatnce I have an application that runs on XP and CE but uses different code paths for each... but I have to put the #define at the top of each file... surely there is a way to set it up like in c++..

thanks


Is there a reason you're not using System.Environment.OSVersion.Platform? It's an enum value which has different values for XP and CE. Then you avoid the #define, and you only have to compile one assembly which runs on both, but can take different code paths depending on which OS it's on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this