user393964
user393964

Reputation:

How is a delegate executed

I'm working with some code that I've found online and I don't quite understand delegates yet. I read some articles about it but the examples I've seen are much more simplistic and not very similar. This is the code I'm having trouble with:

var cb = new Action<OAuthAccessToken, TwitterResponse>(CallBackVerifiedResponse);
service.GetAccessToken(_requestToken, pinText.Text, CallBackVerifiedResponse);

Is the new Action object actually executing the CallBackVerifiedResponse method or does that only happen in the second line? OAuthAccessToken and TwitterResponse are the types of the parameters that the CallBackVerifiedResponse method asks for but to me it doesn't look like they're initialized at any point.

Can someone offer me an explanation or an alternative/more simpel way to write those two lines? Here's the full method just in case:

    void CallBackVerifiedResponse(OAuthAccessToken at, TwitterResponse response)
    {
        if (at != null)
        {
            SerializeHelper.SaveSetting<TwitterAccess>("TwitterAccess", new TwitterAccess
            {
                AccessToken = at.Token,
                AccessTokenSecret = at.TokenSecret,
                ScreenName = at.ScreenName,
                UserId = at.UserId.ToString()
            });
        }
    }

Upvotes: 0

Views: 112

Answers (4)

CodingWithSpike
CodingWithSpike

Reputation: 43748

You can think of a delegate as a reference to a method.

In the code:

service.GetAccessToken(_requestToken, pinText.Text, CallBackVerifiedResponse);

the CallBackVerifiedResponse method is not actually executed. If there were parenthesis after it:

CallBackVerifiedResponse() then the method would be executed, but without the parenthesis, it is going to pass a reference to the method into service.GetAccessToken() so that that method can execute the method itself.

Upvotes: 0

L.B
L.B

Reputation: 116178

A simple example

DoWork(CallThisWhenFinished);


public void DoWork(Action action) //<--GetAccessToken
{
    MessageBox.Show("DoWork");
    action();
}

public void CallThisWhenFinished() //<--CallBackVerifiedResponse
{
    MessageBox.Show("CallMeWhenFinished");
}

Upvotes: 1

MNGwinn
MNGwinn

Reputation: 2394

Neither. The code inside GetAccessToken will call the CallBackVerifiedResponse method at some point. Or not. Depends on the code in GetAccessToken. I asssume it'll call it when the response has been verified.

Basically, a delegate is a way to hand someone a function and say "Make this function call later, when you need it".

OAuthAccessToken and TwitterResponse are initialized within GetAccessToken. That's the goal of GetAccessToken - to asynchronously get you an OAuthAccessToken and tell you about it when it's done. The purpose of the delegate is to provide the API with a mechanism to do that.

Upvotes: 2

O. R. Mapper
O. R. Mapper

Reputation: 20770

Neither the first nor the second line execute the CallBackVerifiedResponse method. In both cases, a method pointer of that method is passed to a method (in the first line, to the Action constructor, and in the second line, to GetAccessToken.

The method may be invoked later on by some code in CallBackVerifierResponse.

Upvotes: 1

Related Questions