1

I have ENUMs and extensions in my application that I use like this:

   

public enum CO
{
    Random = 0,
    FirstToLast = 1,
    LastToFirst = 2,
}

public static partial class Extensions
{
    public static string Text(this CO cardOrder)
    {
        switch (cardOrder)
        {
            case CO.Random: return "Random";
            case CO.FirstToLast: return "1, 2, 3";
            case CO.LastToFirst: return "3, 2, 1";
        }
        return "";
    }
}

In the codes I have switch statement set to decide to update a database:

    

switch (segControlCardOrder.SelectedValue)
{
   case "Random":
         App.DB.UpdateIntSetting(SET.Co, (int)CO.Random);
         break;
   case "1, 2, 3":
         App.DB.UpdateIntSetting(SET.Co, (int)CO.FirstToLast);
         break;
   case "3, 2, 1":
         App.DB.UpdateIntSetting(SET.Co, (int)CO.LastToFirst);
         break;
}

Is there a way that I could avoid using the switch statement and just call the UpdateIntSettings based on the value of the ENUM?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • You already seem to be passing the enum value into the UpdateIntSetting method so it certainly feels like you don't need to get the string from your Extension method to then use in teh switch but can instead just pass that enum into the method. Its hard to say if that would work for you though since its not clear if you actually have the enum value at the point you call your second switch statement. – Chris Jul 30 '18 at 10:59
  • Consider using an attribute against the enum to store the string - https://stackoverflow.com/questions/2650080/how-to-get-c-sharp-enum-description-from-value . – mjwills Jul 30 '18 at 11:11

3 Answers3

3

You can also add an extension method for the other way (from string to enum). Then you can use the method in you statement:

public static CO CardOrder(this string cardOrder)
{
    switch (cardOrder)
    {
        case "Radom": return CO.Random;
        case "1, 2, 3": return CO.FirstToLast;
        case "3, 2, 1":  return CO.LastToFirst;
    }
    throw new ArgumentException(string.Format($"{cardOrder} is not a CO representation"));
}

Simple use:

App.DB.UpdateIntSetting(SET.Co, (int)segControlCardOrder.SelectedValue.CardOrder());
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
PinBack
  • 2,499
  • 12
  • 16
  • This is a really good practice, especially if you find yourself reusing the same switch statement. It's quicker and if you want to add more cases, you only have to add them in one spot and the rest of the usages won't need to be changed. – AustinWBryan Jul 31 '18 at 13:42
1

You can initialise a Dictionary<string, CO> like as private or static member of the class:

dict.Add("Random", CO.Random);
dict.Add("1, 2, 3", CO.FirstToLast);
dict.Add("3, 2, 1", CO.LastToFirst);

And finally do:

App.DB.UpdateIntSetting(Set.CO, (int)dict[segControlCardOrder.SelectedValue]);
mnieto
  • 3,744
  • 4
  • 21
  • 37
1

Yes, but it is less efficient. You can use a dictionary of the values, like:

var dict = new Dictionary<string, CO> { ["Random"] = CO.Random, ["1, 2, 3"] = CO.FirstToLast, ["3, 2, 1"] = CO.LastToFirst };
App.DB.UpdateIntSetting(SET.Co, (int)dict[segControlCardOrder.SelectedValue]);

Or simply set the value to using in a variable, and pass it to the method. I prefer the last way.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77