3

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 fnCommand;

            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 ?

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

-1. It doesn't answer his question, in fact you repeated his question!
@Nawaz: What he really wants to add commands to his Console class and access them if I am right. What he's trying to do is plainly 'translate' it to C#.... functional language to object-oriented language!!! What he does is a solution but not the optimal solution. All I'm doing is pointing him to the right direction. To make it more plain to you, he wants to convert a C++ (looks like C to me, unless he provides a code to show it's C++) to C#.
You're right, I was converting without thinking. Thanks for the hint. Though, I'm not sure whether a dictionary is faster, but I'll try.
3

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) 
   };

4 Comments

Just beat me to it - the point here is that you need to write the this for member function delegates (or otherObject.Nick). On your other point, no all C# arrays are object types and so need to be 'new'ed.
OK, true, I get "Cannot use 'this' in a member initialiser". If you move the initialisation into a constructor it'll work. And actually it seems to work there even if you drop the 'this' - odd, I didn't know that.
@Rup: The last thing is not odd, it's the default behaviour in a constructor.
Csharp seems to have the same issues with member-functionpointers as C++: this->(*this->functionpointer)
1

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

2 Comments

The idea that structs are allocated on the stack and classes on the heap is, at best, sometimes true. See blogs.msdn.com/b/ericlippert/archive/2010/09/30/….
Yes, that's why I said "analagous" rather than "equivalent". The value type vs. reference type is the more important distinction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.