The equivalent of Global constants in VB

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
 
M

Morten Wennevik

You wouldn't have to instantiate the class since a public const would be
static too.
 
J

Jon Skeet [C# MVP]

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
 
N

n!

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

Mark Rae

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)
 
N

n!

And yes, I just remembered const values are implicitly static, so no need to
use the static keyword (gah). :)

n!
 
J

Jon Skeet [C# MVP]

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.)
 
M

Mark Rae

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

n!

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top