简体   繁体   中英

Combine two Method in C#

So i have following two method's, i was thinking is there any way i can reduce the code to one method and optimize code

Both Method's are almost same except the if statement

private void RepeatSearch()
{
  string optionRead = string.Empty;
  do
  {
   Console.WriteLine("\nPress \"Y\" to Continue ,\"M\" For Main Menu\n");
   Console.Write("Your Choice : ");
  optionRead = Console.ReadLine().ToLower();
   if (optionRead == "y")
    {
     SearchData();
    }
    if (optionRead == "m")
     {
       m.SelectOption();
     }
   else
     {
      Console.ForegroundColor = ConsoleColor.Red;
      Console.WriteLine("\nInvalid Option.Enter M or Y\n");
      Console.ResetColor();
     }
  } while (optionRead != "m" || optionRead != "y");
}

private void RepeatAdd()
{
  string optionRead = string.Empty;
  do
  {
   Console.WriteLine("\nPress \"Y\" to Continue ,\"M\" For Main Menu\n");
   Console.Write("Your Choice : ");
  optionRead = Console.ReadLine().ToLower();
   if (optionRead == "y")
    {
      AddData();
    }
    if (optionRead == "m")
     {
       m.SelectOption();
     }
   else
     {
      Console.ForegroundColor = ConsoleColor.Red;
      Console.WriteLine("\nInvalid Option.Enter M or Y\n");
      Console.ResetColor();
     }
  } while (optionRead != "m" || optionRead != "y");
}

So pass the difference in as a delegate

private void DoASearch(Action a)
{
    string optionRead = string.Empty;
    do
    {
        Console.WriteLine("\nPress \"Y\" to Continue ,\"M\" For Main Menu\n");
        Console.Write("Your Choice : ");
        optionRead = Console.ReadLine().ToLower();
        if (optionRead == "y")
        {
            if(a != null)
            {
                a();
            }
        }
        if (optionRead == "m")
        {
            m.SelectOption();
        }
        else
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("\nInvalid Option.Enter M or Y\n");
            Console.ResetColor();
        }
    } while (optionRead != "m" || optionRead != "y");
}

then

DoASearch(SearchData);

What you should do is parameterize the methods, by making parameter(s) of all difference between two equal functions.

Eg

private void Repeat(bool add) // True when adding, false when searching
{
    ...
    if (add)
    { 
        AddData();
    }
    else
    {
        SearchData();
    } 
}

Use an if statement depending on the bool add.

Btw, it would be better to use an enum instead of a boolean.

Also, in your case (as the solution of spender writes), a delegate suffices. Using parameters like in my solution is a more generic solution.

You can pass a boolean variable to the method to denote whether it shoud do SearchData(); or AddData();.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM