Constant declares
-
I need some help understanding basic c# layout regarding constant declarations. I have some global constants I'd like to declare for my windows.form based app such as: const int APPLICATION_STATE_PAUSED = 0; const int APPLICATION_STATE_RUNNING = 1; etc... Where is the correct place to declare these so they will be accessable by all forms and classes in my project? Thanks - mutty
-
I need some help understanding basic c# layout regarding constant declarations. I have some global constants I'd like to declare for my windows.form based app such as: const int APPLICATION_STATE_PAUSED = 0; const int APPLICATION_STATE_RUNNING = 1; etc... Where is the correct place to declare these so they will be accessable by all forms and classes in my project? Thanks - mutty
The only way to make them globally visible is to create a class that has them as public static members. There is no global scope in C#. Personally, I think this looks like C code, I'd ditch the all caps syntax, and maybe even use an enum instead. const is only good for compile time values, for run time values, they need to have class scope, and be declared in the constructor, and they need to be readonly, not const. Christian Graus - Microsoft MVP - C++
-
The only way to make them globally visible is to create a class that has them as public static members. There is no global scope in C#. Personally, I think this looks like C code, I'd ditch the all caps syntax, and maybe even use an enum instead. const is only good for compile time values, for run time values, they need to have class scope, and be declared in the constructor, and they need to be readonly, not const. Christian Graus - Microsoft MVP - C++
-
Same again, there is no such thing as globally visible, excepting public and static on a class that you use to hold this sort of stuff. Or just outside any class, if they all have the same namespace. Christian Graus - Microsoft MVP - C++
-
I need some help understanding basic c# layout regarding constant declarations. I have some global constants I'd like to declare for my windows.form based app such as: const int APPLICATION_STATE_PAUSED = 0; const int APPLICATION_STATE_RUNNING = 1; etc... Where is the correct place to declare these so they will be accessable by all forms and classes in my project? Thanks - mutty
Create a struct that has static accessors to the constants you want to define. Such as: public struct Aprori { static public int Running { get { return _Running; } } private const int _Running = 5; } ... Then access it by int x = Aprori.Running; That way you have one object that contains your constants and is easily accessed. If you have differing constants you can group them in other structs for similar purpose.