Where to define constants and use them anywhere?
In Java, I would write something like
public interface ICar {
static String RED_CAR = "/res/vehicles/car_red.png";
static String BLUE_CAR = "/res/vehicles/car_blue.png";
static String GREEN_CAR = "/res/vehicles/car_green.png";
}
Since C # does not allow the use of fields in the interface, where can I define these constants in C # if I want to use them more than once?
source to share
You can define a static class to contain your constants (which should be public
the default, no modifier private
):
public static class Car
{
public const string RED_CAR = "/res/vehicles/car_red.png";
public const string BLUE_CAR = "/res/vehicles/car_blue.png";
public const string GREEN_CAR = "/res/vehicles/car_green.png";
}
After that, you can use type constants Car.RED_CAR
.
source to share
If all of your constants are files, it's best to include them in your resources.
There is a resources section in your project properties, vs can create a Resource.resx if you need it. There you can add all sorts of files or strings (for translations mostly).
Then you can access them via Properties.Resources.RED_CAR
I would not call them that. This is from the time when all variables, where global and naming conventions like this, where you need to know what was stored in the variable. But when you access your data like this, it's always clear what's going on.
source to share