> but can anyone tell me how to simulate the concept of a global constant in
a
C# Windows app? The app in question contains several forms, each of which
need to interrogate the value of a "global" constant. Do I have to create
a class with a public constant declaration and instantiate that class from
each form?
You need to declare a class, but no need to instantiate it anywhere. The
constants become available by making them public static members. e.g.
namespace MyAppNamespace
{
public sealed class MyGlobalConstan ts
{
private MyGlobalConstan ts() // Prevent instantiation
{
}
public static const int ConstantOne = 53;
public static const int ConstantTwo = 56;
public static const string StringConstant = "A
Constant";
}
}
Which may be accessible from anywhere in your app via:
namespace MyAppNamespace
{
public class MyClass
{
private int myValue;
private string myString;
public MyClass()
{
// Access a constant...
myValue = MyGlobalConstan ts.ConstantOne;
myString = MyGlobalConstan ts.StringConsta nt;
}
}
}
static members (shared in VB) do not require a class instance in order to be
accessed.
Note, if the constants are all integer values an enumeration may be a better
choice.
n!