SkyeBoniwell SkyeBoniwell - 1 year ago 79
C# Question

How build constructor for static class

Is there a way to wrap up a bunch of static fields into its own class?

I tried doing something like this, but I can stop at the constructor.

What should I pass into the constructor?

public static class CarPartsLibary
private static string factoryId = "fgk4548j4t5jig3";
private static string engineId = "fgfgj34lk45l3lj";
private static string bodyId = "45kjgiu590dkjr";
private static bool typeCar = true;
private static bool autoStart = true;
private static bool allWheelDrive = true;
private static bool dieselEngine = false;

static CarPartsLibary()

I want to put all the static fields into its own class so that I can do stuff like this:

var pl = CarPartsLibary();

var car = pl.typeCar;
var factory = GetLocation(pl.factoryId);


Answer Source

When having a static class one does not have instances of it and accesing it is through the: ClassName.FieldName. (Also to access the fields they way you wanted they should be public)

public static class CarPartsLibary
    public static string factoryId = "fgk4548j4t5jig3";
    public static string engineId = "fgfgj34lk45l3lj";
    public static string bodyId = "45kjgiu590dkjr";
    public static bool typeCar = true;
    public static bool autoStart = true;
    public static bool allWheelDrive = true;
    public static bool dieselEngine = false;

var car = CarPartsLibary.typeCar;
var factory = GetLocation(CarPartsLibary.factoryId);

Read more on MSDN about static classes and using them

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download