3

I'm checking name of the file and return TRUE if it's correct:

bool name_FORD = file.Contains("FORD"); 
bool name_KIA  = file.Contains("KIA");  
bool name_BMW  = file.Contains("BMW");

Based on this I want to have switch and run correct method. But I confused how to correctly do it:

switch (true)
{
 case 1 name_FORD: 
              method1();
              break();
 case 2 name_KIA:
              method2();
              break();
 case 3 name_BMW:
              method3();
              break();
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
4est
  • 3,010
  • 6
  • 41
  • 63

2 Answers2

4

I suggest organizing all strings and corresponding methods as a Dictionary:

Dictionary<string, Action> myCars = new Dictionary<string, Action>() {
  {"FORD", method1}, // e.g. {"FORD", () => {Console.WriteLine("It's Ford!");}},
  { "KIA", method2},
  { "BMW", method3}, 
  //TODO: Put all the cars here
};

then we can put a simple loop:

foreach (var pair in myCars)
  if (file.Contains(pair.Key)) { // if file contains pair.Key
    pair.Value();                // we execute corresponding method pair.Value

    break; 
  }

Edit: In case we can have complex methods (e.g. method may want file and key parameters) we can change signature:

// Each action can have 2 parameters: key (e.g. "FORD") and file
Dictionary<string, Action<string, string>> myCars = 
  new Dictionary<string, Action<string, string>>() {
     {"FORD", (key, file) => {Console.Write($"{key} : {string.Concat(file.Take(100))}")}}, 
     { "KIA", (key, file) => {Console.Write($"It's {key}!")}},
     { "BMW", (key, file) => {/* Do nothing */}}, 
  //TODO: Put all the cars here
};

When executing in the loop, we should provide these parameters:

foreach (var pair in myCars)
  if (file.Contains(pair.Key)) { // if file contains pair.Key
    pair.Value(pair.Key, file); // we execute corresponding method pair.Value

    break; 
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • where the method will be executed?? – 4est Jul 02 '19 at 11:13
  • @4est: `pair.Value();` we get `pair.Value` which is `Action` and execute it - `()` – Dmitry Bychenko Jul 02 '19 at 11:13
  • I did simple method to test: private void method1() => Console.WriteLine("test"); but it's not working – 4est Jul 02 '19 at 11:15
  • Let's test: `{"FORD", () => {Console.WriteLine("test");}}, ...` – Dmitry Bychenko Jul 02 '19 at 11:16
  • hi, @ Dmitry, one more: i got complex method and getting this: **Argument2: cannot convert from 'method' group to 'Action'** – 4est Jul 02 '19 at 12:18
  • Try *wrapping* the method into `Action`, e.g. `{"FORD", () => {myComplexMethod(argument1, argument2, argument3);}},` here we can call everything we like within `{...}`. If you want to pass `file` to action, declare it as `Dictionary> myCars = new Dictionary>()` and call `pair.Value(file); ` – Dmitry Bychenko Jul 02 '19 at 12:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195859/discussion-between-4est-and-dmitry-bychenko). – 4est Jul 02 '19 at 12:41
0

You can use methods like variables in c# by assigning them to an Action:

public void KiaMethod(){
  Console.WriteLine("Kia");
}
public void BmwMethod(){
  Console.WriteLine("BMW");
}

Action method = null;
if(file.Contains("KIA"))
  method = KiaMethod;
else if(file.Contains("BMW"))
  method = BmwMethod;

method();

Though I really do favor the pattern in Keiran's answer because I'm not really seeing why you need this level of complexity

Caius Jard
  • 72,509
  • 5
  • 49
  • 80