开发者

Problem converting array of struct from C++ to C#

I want to convert this C++ code to C#:

typedef struct consoleCommand_s 
{
    char* cmd          ;
    void (*function)() ;
} consoleCommand_t     ;



static consoleCommand_t commands[] = 
{
    {"clientlist", &CG__Clientlist},
    {"say", &CG_Say},
    {"nick", &CG_Nick},
    {"logout", &CG_Logout}
} ;

// e.g.
static void CG_Logout(void)
{
    // Logout
}

The closest i have come is this:

 public class Class1
    {
        // public delegate int Calculate (int value1, int value2);
        public delegate void fnCommand_t();

        public class consoleCommand_t
        {
            public string strCommandName; 
            public fnCommand_t fnComman开发者_如何学JAVAd;

            public consoleCommand_t(string x, fnCommand_t y)
            {
                this.strCommandName = x;
                this.fnCommand = y;
            } // End Constructor

        } // End Class consoleCommand_t


        public static void Nick()
        {
            Console.WriteLine("Changing Nick");
        } // End Sub


        public static void Logout()
        {
            Console.WriteLine("Logging out");
        } // End Sub

        // string[] names = new string[] {"Matt", "Joanne", "Robert"};
        public consoleCommand_t[] commands = new consoleCommand_t[] {
            new consoleCommand_t("Nick",   Nick),
            new consoleCommand_t("Logout", Logout)
        };





    } // End Class Class1

Now I wanted to ask:

A) Why does Nick & Logout need to be static when all the rest is not ?

B) Is there no C-like way to initialize the commands array, that is, without new ?


You could do without having a class INSIDE another class and manage your default console commands in another.

public delegate void ConsoleCmd();

public class DefaultCommands
{
    public static void Nick()
    {
        Console.WriteLine("I'm Nick!");
    }

    public static void LogOut()
    {
        Console.WriteLine("You're Fired!");
    }
}

public class Console
{
    private Dictionary<string, ConsoleCmd> mCommands;

    public Console()
    {
        mCommands = new Dictionary<string, ConsoleCmd>();
        mCommands.Add("Nick", DefaultCommands.Nick);
        mCommands.Add("Logout", DefaultCommands.LogOut);
    }
}

Accessing your commands would be as easy as:

ConsoleCmd command = mCommands["Nick"];
command();

EDIT: Failed to mention this. This is to point to the OP that there are other better methods to achieve what he wants to do. And probably doesn't answer his question but I hope will point him to the right direction in terms of his switch from functional programming language to purely object-oriented language.


Nick & Logout may be non-static:

    public void Nick(){}
    public void Logout(){}

   public consoleCommand_t[] commands = new consoleCommand_t[] {
        new consoleCommand_t("Nick",   this.Nick), 
        new consoleCommand_t("Logout", this.Logout) 
   };


B) Is there no C-like way to initialize the commands array, that is, without new ?

Don't get hung up on the new keyword in C#, it's just how initialisation is written. In C#, the analagous distinction between stack allocation and heap allocation is made using struct vs class. (So you might like to make your ConsoleCommand_t a struct.)

Edit: And with a struct you can also do:

new consoleCommand_t() {strCommandName = "Nick", fnCommand = Nick}

And skip writing the consoleCommand_t constructor. That's about as close as you can get to your C-style struct initialization, I think

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜