0

Here is what my app has:

private void saveInDatabase(string key, string value)   

 switch (key)
 {
   case "a":
      def = Helpers.Utils.listOfDoubleFromString(value);
      break;
   case "b":
      chi = int.Parse(value);
      break;
            ....
   }

Is there a way that I could use a dictionary like this to decide what actions happen with the different case values?

   {"a", () => def = Helpers.Utils.listOfDoubleFromString(value)},
   {"b", () => chi = int.Parse(value)},

My app has a large number of these case statements so I am interested to know if I could replace them by setting up a dictionary and then some more code.

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • It's possible to map a key to a Func but how would you know what the return type is going to be? `dict[key](value);` – ChiefTwoPencils Aug 19 '20 at 16:22
  • Sorry, I made a mistake when trying the mapping. I need to return to different things like def or chi. – Alan2 Aug 19 '20 at 16:28
  • And def and chi are available when you build the dictionary? – ChiefTwoPencils Aug 19 '20 at 16:29
  • 2
    this is a pure C# question, nothing to do with Xamarin – Jason Aug 19 '20 at 16:30
  • https://stackoverflow.com/questions/126409/ways-to-eliminate-switch-in-code – vin Aug 19 '20 at 16:32
  • You'd have to create the dictionary in the method in order to capture those variables assuming they are local. Then it's just a `Dictionary`. – juharr Aug 19 '20 at 16:40
  • You use a dictionary to reduce the search time from N/2 to a has which is log(N). Using a dictionary here where you need to search for ranges will not help unless you can map data to a BIN. For example it you have floating point numbers and can round number to an integer than a dictionary will speed up results. – jdweng Aug 19 '20 at 16:40
  • Def and Chi have been defined already and have values. – Alan2 Aug 19 '20 at 16:50

1 Answers1

1

I mean you pretty much got it:

string def;
int chi;

var map = new Dictionary<string, Action<string>>
{
    ["a"] = value => def = Helpers.Utils.listOfDoubleFromString(value),
    ["b"] = value => chi = int.Parse(value),
};

map[key](value);
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • It is not so clear what the asker wants. You let your lambdas capture `def` and `chi` which are defined in a "containing scope", but not catch `value`. Depending on what `value` is, it may also be captured, and then you would have `Action` instead of `Action`. If you use your approach, maybe rename the string parameter in the lambdas to just `v`, but keep `value` in the invocation in the last line. – Jeppe Stig Nielsen Aug 19 '20 at 17:09
  • @Blindy - Could this be changed to Dictionary ? I have no need to capture any value. – Alan2 Aug 19 '20 at 17:14
  • @Alan2, but if you make it an `Action` you'll have to capture `value`; how else would you be able to use it? – ChiefTwoPencils Aug 19 '20 at 17:22
  • @Alan2, As you wish, if `value` is a field for example, it could be curried over instead of explicitly sent. These are the kinds of details that become clearer with actual code samples, instead of `int.Parse`. – Blindy Aug 19 '20 at 17:27
  • Helpers.Utils.listOfDoubleFromString(value) and intParse(value) are copied from the application code. Variable names def and chi are just short versions of longer variable names that are defined like App.CM.Selectedconf.newChi.delays. value is a parameter passed into the method where the switch appears. – Alan2 Aug 19 '20 at 17:37