1

I am aware I can do this:

WebClient client = new WebClient();
client.UploadStringCompleted += delegate(object sender, UploadStringCompletedEventArgs e)
{
    //handle event
};
client.UploadStringAsync(myURI, "POST", "some_data");

But is there a way I can pass an inline delegate as an argument? Something like this:

DoRequest("some_data",
    delegate(object sender, UploadStringCompletedEventArgs e)
    {
        //handle event
    });

public void DoRequest(string data, UploadStringCompletedEventHandler event)
{
    WebClient client = new WebClient();
    client.UploadStringCompleted += event;
    client.UploadStringAsync(myURI, "POST", data);
}

2 Answers 2

3

Yes, that code is correct, except you can't call your parameter event. I'd also use a lambda expression instead because it's nicer.

DoRequest("some_data", (o, e) => {/* handle event */});

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

Comments

2

Yes, you can write exactly that except that event is a keyword and can't be used as a variable name.

1 Comment

Yeah I noticed that. Before, I was wrapping an event cast around one of my lines which messed things up. Thanks

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.