views:

510

answers:

2

I am trying to do as the title suggests, however I am getting confused.

I get the idea of how the delegate is supposed to work, but Visual Studio is telling me i'm wrong. Microsoft documentation that says how to do this contains a convoluted example that uses a bookstore program that contains templates and a bunch of logic code that makes it hard to follow.

How do you do this? Thanks.

+2  A: 

A delegate is a safe function pointer, you should be assigning a method to a declared variable of that name not trying to assign the type itself which you are doing.

class MyGameClass
{
    SetGameAreaDelegate handler;
    MyGameClass()
    {
        // Instantiate the handler (since my callback is a non-static method)
        // You'll need to preform this assignment in the constructor, 'this'
        // is not valid during initialization 
        handler = new SetGameAreaDelegate(myGameAreaWithCallback);
        handler = MyGameAreaWithCallback; // short for above
    }
    void MyGameAreaWithCallback(Game1.gameAreas newArea)
    {
        //...
    }
}

Update: Details about delegates

A delegate is a managed wrapper for function pointers. It has it's own type signature and is presumably a save alternative to just raw function pointers. A delegate can hold a reference to an instance object like a C++ style member function pointer but you never have to worry about this because the run-time figures out this kind of information for you.

It might be good to know that a delegate to a non-static method will track a reference to that object. This can result in memory not being garbage collected because delegates while they might look harmless, maintain or track object references.

The problem with your code is that the type signature...

void SetGameAreaWithCallback(Game1.gameAreas newArea, SetGameAreaDelegate callback)

...doesn't match your delegate type...

delegate void SetGameAreaDelegate(Game1.gameAreas newArea);

...for this to work...

SetGameAreaDelegate handler = SetGameAreaWithCallback;

...your delegate should have been...

delegate void SetGameAreaDelegate(Game1.gameAreas newArea, SetGameAreaDelegate callback);

...you forgot a parameter if this is what you really meant, that is why the method resolution fails.

John Leidegren
John, I'm obviously not understanding you correctly because what you did above it exactly what I already have :/
Brock Woolf
Right, I overlooked something, well the type signature doesn't match... you have an additional parameter in your method which isn't present in your delegate. "SetGameAreaDelegate callback"
John Leidegren
+4  A: 

I assume, in your example, that you want the SetGameAreaWithCallback method to actually call the changeGameArea method on an instance of Game1.

To do this you need to create your delegate instance so that it refers to that method:

// game1 is the instance of the Game1 class that you want to call
// Instantiate the handler
SetGameAreaDelegate handler = new SetGameAreaDelegate(game1.changeGameArea);

If you're using C#2 or above then the syntax is even simpler:

// game1 is the instance of the Game1 class that you want to call
// Instantiate the handler
SetGameAreaDelegate handler = game1.changeGameArea;
LukeH
Thank you Luke, that was spot onMuch simpler than what I was trying to do :D
Brock Woolf