C# delegates functions being defined in an array declaration? -


i making irc chat bot stream. found few basic connectivity examples using c# decided give try.

so far love it

but stuck on 1 part.

i want store bot commands inside array of structure type.

public delegate void cmdhandler(string[]);  struct botcommand {     string name;     cmdhandler chandler;     bool isadmin = false;     string = "nothing here."; } 

is have, , want beable this:

botcommand[]commands = {     { "testcommand", testcommand(), 0, "help this" },     { "testcommand2", testcommand2(), 0 "..." },     ...... }; 

so how link generic function in array?

or going wrong way?

basically instead of having giant switch() statement check command used want loop through array , see if command in there. if call function associated command.

edit:

this have can see trying do

    public delegate void cmdhandler(string[] ex);      struct botcommand     {         string name;         cmdhandler chandler;         bool isadmin = false;         string = "nothing here.";     }      botcommand[] commands =     {         {"test",  new cmdhandler(testf), 0, ""  }     };      public void testf(string[] ex) {         return;     } 

steps of logic:

  1. user enters test command
  2. loop through botcommands see if find test command
  3. test command found
  4. call function associated test command , pass on argument (the rest of command)

to me seems you're mixing c/c++ concepts c# (using struct instead of class, 0 false, object initializers, etc...).

to solve individual problem, must instantiate struct differently.

botcommand[] commands = new [] {     new botcommand {         name = "test",         chandler = new cmdhandler(mymethod),         isadmin = false,         = "no you..."     } }; 

where mymethod defined as.

public static void mymethod(string[] myargs) {     //... ... } 

however, think better approach have abstract class / interface individual command, list or dictionary of commands.

public interface ibotcommand {     string name { get; }     bool isadmin { get; }     void process(string[] args);     string helptext { get; } }  public class mycommand : ibotcommand {     string name      {                 {             return "nameofthecommand";         }     }      bool isadmin      {                   {              return false;          }      }      void process(string[] args)     {         // bla bla, im processing stuff     }      string helptext      {                   {              return "this text";          }      } } 

and using it.

list<ibotcommand> commands = new list<ibotcommand>();  commands.add(new mycommand());  // find command , execute ibotcommand cmdtoexecute = commands.singleordefault(c => c.name == "nameofthecommand");  if (cmdtoexecute != null) {     cmdtoexecute.process(args); // where-ever args comes } else  {     // unknown command "nameofthecommand" } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -