I'm creating a custom Input dialogue in C#. My input dialogue has multiple input fields (as specified by the constructor), and passes all of the input to a delegate method when submitted.
I would also like the caller to be able to send a method to the input dialogue via a parameter. However, I'm having a bit of trouble figuring this out. Here is my code:
The InputDialogue
class:
public class InputDialogue {
public static InputDialogue ins;
public delegate void Foo(string[] input);
public Foo doThisWithTheData;
public InputField[] fields;
public static void Query(string title, string[] fields, MethodToCall m)
{
// display the dialogue, allowing the user to input data into the fields
doThisWithTheData = m;
}
public void Submit()
{
List<string> input = new List<string>();
foreach (InputField i in ins.fields)
{
input.add(i);
}
doThisWithTheData(input.ToArray());
}
}
The methods I want to pass as arguments:
public class UserProfile
{
public static void Login(string[] input)
{
string user = input[0];
string pass = input[1];
ValidateCredentials();
}
public void ChangeName(string[] input)
{
if (ValidatePassword(new string[] { input[0] }))
{
name = input[1];
WriteToFile();
}
else
MessageDialog.Set("Error", "Invalid password.");
}
public void ChangePassword(string[] input)
{
if (ValidatePassword(new string[] { input[0] }))
{
password = input[1];
WriteToFile();
}
else
MessageDialog.Set("Error", "Incorrect password");
}
}
Some example calling statements:
InputDialogue.Query("Login", new string[] { "Name", "Password" }, UserProfile.Login);
InputDialogue.Query("Change Name", new string[] { "New Name", "Password" }, UserProfile.ChangeName);
InputDialogue.Query("Change Password", new string[] { "Current Password", "New Password" }, UserProfile.ChangePassword);
I understand that I could simply have the caller manually set doThisWithTheData
, which technically would work, but I want to wrap this all in a method. Thus, my main question is how I can pass my methods into Query
as arguments. How can I achieve this?