Home > Blockchain >  Best ways to save constants in C#
Best ways to save constants in C#

Time:01-10

I want to save a number of constants, and these should always works as one group, ex: when I call the first group, should get all the data from the first group and I have to take this data and save it in an excel file.

Group One:
speed = 45,
color = 12,
height = 23

Group Two:
speed = 450,
color = 128,
height = 13

Group Three:
speed = 15,
color = 542,
height = 23

Is the best way to save these values in enum or List of type Tuple?

Enum:

        public enum Group_One
        {            
            speed = 45,
            color = 12,
            height = 23
        }

List

List<Tuple<string, string>> Group_One= new List<Tuple<string, string>>
            {
                Tuple.Create("speed", "45"),
                Tuple.Create("color", "12"),
                Tuple.Create("height", "23")
            };

CodePudding user response:

Create a class:

public class ConstantsCollection
{
   public int Speed {get;}
   public int Color {get;}
   public int Height {get;}

   public ConstantsCollection(int speed, int color, int height)
   {
     Speed = speed;
     Color = color;
     Height = height;
   }
}

Then create your three groups:

public static readonly ConstantsCollection group1 = new ConstantsCollection(45, 12, 23);
// same for the other groups

You can think of a better name for your class. From your question, I don't know what your constants represent. It has a speed, so maybe a vehicle? In that case, call your class Vehicle.

CodePudding user response:

Another way of keeping them together and using them as groups is using dictionaries as follows:

        var groups = new Dictionary<string, Dictionary<string, int>>();

        var firstGroupValues = new Dictionary<string, int>
        {
            { "spped", 45 },
            { "color", 12 },
            { "height", 13 }
        };
        groups.Add("Group One", firstGroupValues);

Then, you can use them as follows: var result = groups["Group One"].

CodePudding user response:

Personally I'd use a class, something like this:

public class MyClass //call it something useful
{
    public int Speed {get;}
    public int Color {get;}
    public int Height {get;}
    //Any other associated variables

    
    public MyClass (int group) // You can use this constructor for the default groups
    {
       if (group == 1) {
           //set defaults for group one
       }
       else if (group == 2){
           //set defaults for group two
       }
       else if (group ==3) {
           //set defaults for group three
       }
       else {
           //deal with the invalid group number entry
       }
    }
    
    public MyClass (int speed, int color, int height) //constructor for different groups
    {
        Speed = speed;
        Height = height;
        Color = color;
    }
}

To create your default 3 groups you'd then just do something along the lines of:

List<MyClass> groups = new List<MyClass>();
for (int i = 1; i <=3; i  ) 
{
    groups.Add(new MyClass(i)); 
}

There are alternatives, depending on your use case (which you've been relatively vague about), but this way provides the most flexibility (and, I guess, future-proofing).

  •  Tags:  
  • Related