I'm looking for a following pattern:
I have several methods like this:
1. private bool CheckIfStringExist(string lookupString, ref string errorMessage)
2. private bool NumbersAreEqual(int one, int two, ref string errorMessage)
...
(around 15 of those).
What I want to do is to run them in a specific order and output error message to a user if any of those failed. Also I want to display method friendly names to a user.
So far, I came up with the following.
i create a class to hold each method separately:
public class CheckingMethod
{
public string name {get; set;}
public Action method {get; set;}
public bool success {get; set;}
public CheckingMethod(string name, Action method)
{
this.name= name;
this.method = method;
this.success = false;
}
}
This allows me to store all of the methods in a list...like this:
List<CheckingMethod> cms = new List<CheckingMethod>();
cms.Add(new CheckingMethod("Check if text exist", ()=>CheckIfStringExist(lookupString, ref errorMessage);
cms.Add(new CheckingMethod ("Check if numbers are equal", ()=>NumbersAreEqual(num1, num2, ref errorMessage);
Then I can run them one by one like this:
foreach (CheckingMethod cm in cms)
{
cm.method()
}
Unfortunately Action returns only void type, so I have no way of telling whether my method returned false or true. Also, I need to prohibit running of the next method if previous returned false (not always though, just in some cases).