Reputation:
I have C++ code which uses such approach:
#if defined(CONSTANT)
..
// Some code
#else
// Some other code
I was told I can use similar approach in C#-because I need to rewrite this C++ project to C#. But there is one problem. From the documentation I have seen that in C# if I use
#define CONSTANT
this is only visible in the file where it was declared.
But I don't want this. I want this CONSTANT
to be visible in all classes?
I think one solution is to declare such constants in the Project Settings etc - but here is my first question: In that case do I need to ship some additional file with my DLL? Or these constants will be embedded in DLL?
Finally, to avoid above problems I am thinking about approach of using just public const values in C#. Like
if(Globals.SomeConstant == SOMEVALUE)
// Do this
else
// Do smth else
And then depending on the configuration I will set default values during declaration of Globals.SomeConstant
to the value I need, compile the DLL and ship it. Does this sound ok and will it work like this? Will the default values be assigned and read properly inside DLL methods? (will they work like #ifdefs
?)
I know I will need to recompile to change the code but that is ok.
Upvotes: 1
Views: 496
Reputation: 191
If you are using Visual Studio is pretty simple to do preprocessor directives.
If you are not using Visual Studio just follow this topic.
For learning purpose about preprocessor directives you can go to Tutorialspoint C# website.
To write code in C# using C++ approach you can use the following syntax:
#define Name_Value
#if (Name_value)
{
//code
}
If this doesn't fit your needs you can go with a configuration file where you can stack in all your key name - value and access them by ConfigurationManger.AppSettings["Key"]
Another way around this is to have a global enum
and assign values.
public enum DemoEnum
{
Name = Value
}
if(passedEnum == DemoEnum.Name)
{
// code
}
The solution you gave with the constants is not very flexible, because if your code changes you will have to create new constants and the image gets too blurry.
Using readonly properties you can switch values of 'const' in your approach.
public class Global
{
public readonly string Name;
public Global()
{
if(condition)
Name = 10;
else
Name = 30;
}
}
If I forgot an option feel free to comment :)
Upvotes: 1
Reputation: 115859
C# does not a have "preprocessor" in the same way C/C++ have. Nor these #define
s are "constants" - they are just "symbols", which you can only query for presence or absence of. They leave no traces in a compiled assembly.
Now, when you compile your assembly with (or without) a #define
d symbol, the result can very well be different:
public const int I =
#if FOO_BAR
42
#else
43
#endif
;
The conceptual difference between C/C++ and C# when it comes to #define
s is that libraries in C/C++ are usually distributed in source form and, when compiled together with your own code, obey global #define
s. In C#, on the other hand, third-party code is most often distributed as a compiled assembly. Thus, if you want to change the way the code in this third-party assembly works, you'll have to do this at runtime.
Upvotes: 2