c# and #define

Started by
8 comments, last by vermilion_wizard 17 years, 4 months ago
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
Advertisement
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?
[TheUnbeliever]
In VS05 look at the Build sub-section of your project properties, there you will find "Conditional Compilation Symbols".
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;
C# has no preprocessor, and therefore no #define
Quote:Original post by DaBookshah
C# has no preprocessor, and therefore no #define

There is support for conditional compilation - so yes #define.
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.
Hmm, my bad, C# has conditional compilation, and #define. You just can't use it in exactly the same way
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.
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.

This topic is closed to new replies.

Advertisement