Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refactor this switch statement?

string liquidClass = string.Empty;

switch (cmbLiquidClass.Text)
{
    case "LiquidClass1":
        liquidClass = Settings.Default.LiquidClass1;
        break;
    case "LiquidClass2":
        liquidClass = Settings.Default.LiquidClass2;
        break;
    case "LiquidClass3":
        liquidClass = Settings.Default.LiquidClass3;
        break;
    case "LiquidClass4":
        liquidClass = Settings.Default.LiquidClass4;
        break;
    case "LiquidClass5":
        liquidClass = Settings.Default.LiquidClass5;
        break;
    case "LiquidClass6":
        liquidClass = Settings.Default.LiquidClass6;
        break;
    case "LiquidClass7":
        liquidClass = Settings.Default.LiquidClass7;
        break;
    case "LiquidClass8":
        liquidClass = Settings.Default.LiquidClass8;
        break;
    case "LiquidClass9":
        liquidClass = Settings.Default.LiquidClass9;
        break;
}

Trying to get in to a local variable the contents of the current class. I can't use a dictionary because of the way strings work (behaving like value types). Is there any way to refactor this so that it doesn't require this many lines to find the chosen liquid class?

like image 922
John Smith Avatar asked Jan 15 '23 18:01

John Smith


1 Answers

You can use the indexer that is part of Settings.Default (tested with .Net 4.0):

var liquidClass = Settings.Default[cmbLiquidClass.Text].ToString();
like image 66
Austin Salonen Avatar answered Jan 26 '23 05:01

Austin Salonen