Reputation: 545
Is it possible to declare global constants? That is, constants that are available in all classes? When I try to declare a constant outside of a class, as I do with an enum, I get a parsing error.
I've been using enums this way for a while, but enums are restricted to integers, and I'd like to use easy-to-use words instead of float values.
Example; I'd like the following to be available in any class:
const float fast = 1.5f;
const float normal = 1f;
const float slow = .75f;
I know i can work around this by creating an enum (Speed) for the speed names, then creating a static method SpeedNum()
that reads enum Speed
and return
s an associated value, but it requires so much extra writing each time and I was hoping for something more elegant:
Ex:
public double function SpeedNum(Speed speed)
{
switch (speed)
{
case speed.fast: return 1.5;
case speed.normal: return 1f;
case speed.slow: return .75f;
}
}
Upvotes: 26
Views: 28709
Reputation: 11
You can set it up by the using
directive, and providing a static class within a namespace. Or outside if you want the properties truly global. This will allow you to make calls to public static members from the Constants
class within the RaceCars
namespace, without prepending them with Constants
like Constants.slow
, instead, it's just slow
.
The static class:
namespace RaceCars
{
public static class Constants
{
public static float slow = 0.5f;
}
}
The calling of a 'constant':
namespace RaceCars
{
using static Constants;
internal class PickupTruck
{
private truckSpeed = slow;
}
}
Upvotes: 0
Reputation: 3252
If you're targeting C# version 6 or higher, and you don't want to use the traditional "static_class_name.Thing", you may use using static, introduced in C# 6.
// File 1
public static class Globals
{
public const string bobsName = "bob!";
}
// File 2
using System;
using static Globals;
class BobFinder
{
void Run() => Console.WriteLine(bobsName);
}
Syntactical sugar. But I find it nifty.
Upvotes: 30
Reputation: 3353
Create a static class e.g. called Constants
containing the constants and access them using Constants.MyConstant
.
public static class Constants
{
public const string MyConstant = "Hello world";
public const int TheAnswer = 42;
}
class Foo
{
// ...
private string DoStuff()
{
return Constants.MyConstant;
}
}
To answer your implied question: You cannot declare constants outside of a class.
Upvotes: 52
Reputation: 172398
MSDN gives the answer to your question as to why you cant use it outside your class:
The const keyword is used to modify a declaration of a field or local variable.
So your field or local variable can be present within a class, this means you cannot have a global const
You can better create a Class with with only constants like this:
public static class GlobalConstant
{
public const float fast = 1.5f;
public const float normal = 1f;
public const float slow = .75f;
}
And then you can use it like this:
class MyProgram
{
public static void Main()
{
Console.WriteLine(GlobalConstant.fast);
}
}
Upvotes: 3