0

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?

  • 1
    Your answer is just one google-search away. – MakePeaceGreatAgain Mar 07 '18 at 07:53
  • I really did do a lot of searching. Most people suggested using System.Action and System.Func....They said that T1 needs to be the type of the argument and T2 needs to be the return type, but my methods return void and void can't be used in that context. – Tanner Fix-It Smith Mar 07 '18 at 08:00
  • If the method returns void, you have to use Action instead of Func. – oliver Mar 07 '18 at 08:07
  • I regret to re-inform myself that I already tried both to no avail. Perhaps I wasn't impementing them properly, but I really did try to find the answer on my own before posting here. – Tanner Fix-It Smith Mar 07 '18 at 11:24

1 Answers1

0
// declare a delegate, or use Action<string[]>
public delegate void MethodToCall (string[] input);

public class InputDialogue 
{     
    // the public here is questionable, esp for a delegate
    public MethodToCall 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;
    }
    ....
}
H H
  • 263,252
  • 30
  • 330
  • 514
  • Thanks, this seems to be working. – Tanner Fix-It Smith Mar 07 '18 at 08:02
  • This is not passing the method for a general UserProfile, but you will have to specifiy a UserProfile instance when passing the delegate, i.e. someUserProfile.Login instead of UserProfile.Login. This is not what @TannerFix-ItSmith requested, at least according to his question. – oliver Mar 07 '18 at 08:11
  • Well, upon reading again, it is not so clear, what he wants... – oliver Mar 07 '18 at 08:14
  • @oliver - not clear what you're on about. When the delegates execute, the UserProfile is known as `this`. – H H Mar 07 '18 at 08:46
  • @HenkHolterman: I thought that he meant passing an Action instead of Action, that is passing the "method group" instead of the method, a conclusion one could come to when looking at his InputDialogue.Query calls. But this assumption seems obsolete, since he has already confirmed that your answer is working. Sorry for the fuss. – oliver Mar 07 '18 at 08:58
  • UserProfile.Login is a static method. I corrected my post to reflect that. With that being said, I think that Henk's answer is correct. – Tanner Fix-It Smith Mar 07 '18 at 11:19