Create a command-only class

Asked

Viewed 172 times

0

I created a class only of commands, so far so good, I pulled the class of commands to the main class but I’m with a mistake

Command class:

public bool OnCommand(string[] args)
{
    if (args.Length > 1)
    {
        string a;
        if ((a = args[1]) != null)
        {
            if (a == "teste")
            {
                Console.WriteLine("teste");
                return true;
            }
        }
    }
    return true;
}

Main Class (I put only one part of the code, I found unnecessary the other part):

public void OnAction(Hashtable parameters)
{
      cmd.OnCommand();
}

Note: I’m wrong in this part: cmd.OnCommand(); says: There is no argument provided that matches the required formal parameter "args"

  • args from where ? shows the code.

  • friend I’m new in c#, I don’t understand what you said

1 answer

0

Possibly args is a string and not a array strings tries to pass this way:

cmd.OnCommand(new string[] { "ArgumentoUm", "ArgumentoDois", "ArgumentoTres" } )

If you want, add params:

 public bool OnCommand(params string[] args)
        {
            if (args.Length > 1)
            {
                string a;
                if ((a = args[1]) != null)
                {
                    if (a == "teste")
                    {
                        Console.WriteLine("teste");
                        return true;
                    }
                }
            }
            return true;
        }

So you can call the function without passing a new string like this:

cmd.OnCommand("ArgumentoUm", "ArgumentoDois", "ArgumentoTres" )
  • But I wanted to like, when I typed test on the console, I would write test, in case I did what Oce said and didn’t show up

  • if ArgumentoDois for teste will appear, i Guess

  • in case it would be like this? test?

  • cmd.OnCommand("ArgumentoUm", "teste", "ArgumentoTres" ) but remember that you must have params the front of the function parameter OnCommand edited the answer

  • Thank you worked, but there is no way to remove the test you write and get registered, and has how to create a check if the command exists?

  • first shows the variable you want to pass as parameter, from where it comes? args =, equal to what ?

  • if Voce is saying this cmd.Oncommand(args); I had misplaced, was without the args

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.