The equivalent of Global constants in VB

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

Another stupid newbie question from me, I'm sorry to say...

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?

Any assistance gratefully received.

Best regards,

Mark Ra
 
You wouldn't have to instantiate the class since a public const would be
static too.
 
Mark Rae said:
Another stupid newbie question from me, I'm sorry to say...

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?

Create a class with a static readonly or const value (depending on
type). For instance:

public class Constants
{
const int OneAndOne = 2;
}

You'd then access that with:

Constants.OneAndOne
 
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 MyGlobalConstants
{
private MyGlobalConstants() // 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 = MyGlobalConstants.ConstantOne;

myString = MyGlobalConstants.StringConstant;
}
}
}

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!
 
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.

Perfect! Thanks very much (and to the other people who replied)
 
And yes, I just remembered const values are implicitly static, so no need to
use the static keyword (gah). :)

n!
 
n! said:
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 MyGlobalConstants
{
private MyGlobalConstants() // Prevent instantiation
{
}

public static const int ConstantOne = 53;
public static const int ConstantTwo = 56;
public static const string StringConstant = "A
Constant";
}
}

Slight error here: consts can't be marked as static because they *have*
to be static.

(They can be private, however - my own sample post mucked up in this
respect; the constant should have been marked as public.)
 
Slight error here: consts can't be marked as static because they *have*
to be static.

Yeah - it genereated an error, so I just removed the word "static" and it's
working fine. Thanks.
 
Slight error here: consts can't be marked as static because they *have*
to be static.

Sorry yes, I did post a correction! I realised the mistake just after the
post was sent.

n!
 
Back
Top