views:

370

answers:

3

Visual Studio 2008, C# 3.0.

I have a method below which calls an event handler. I would like to pass the two arguments received by the method to the event handler.

I would like to do something like this:

wc.DownloadDataCompleted += wc.DownloadedDataCompleted(strtitle, placeid);

Is this even possible, if yes, how would I go about doing it ?

Code Snippet:

public void downloadphoto(string struri,string strtitle,string placeid)
{
    using (WebClient wc = new WebClient())
    {
        wc.DownloadDataCompleted += wc_DownloadDataCompleted;
        wc.DownloadDataAsync(new Uri(struri));
    }
}
+5  A: 

The easiest way to do this is to use an anonymous function (an anonymous method or a lambda expression) to subscribe to the event, then make your method have just the parameters you want:

public void downloadphoto(string struri, string strtitle, string placeid)
{
    using (WebClient wc = new WebClient())
    {
        wc.DownloadDataCompleted += (sender, args) => 
            DownloadDataCompleted(strtitle, placeid, args);
        wc.DownloadDataAsync(new Uri(struri));
    }
}

// Please rename the method to say what it does rather than where it's used :)
private void DownloadDataCompleted(string title, string id, 
                                   DownloadDataCompletedEventArgs args)
{
    // Do stuff here
}
Jon Skeet
Awesome !! now who would have thunk that !!
dezkev
+2  A: 

DownloadDataAsync has an overload which takes an object:

DownloadDataAsync(uri, object)

That object can be any arbitrary thing you want that gets passed into the DownloadDataCompleted handler:

public void downloadphoto(string struri,string strtitle,string placeid)
{
    using (WebClient wc = new WebClient())
    {
        string[] data = new string[2] { strtitle, placeid };
        wc.DownloadDataCompleted += wc_DownloadDataCompleted;
        wc.DownloadDataAsync(new Uri(struri), data);
    }
}


void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    string[] data = (string[])e.UserToken;
    string strtitle = data[0];
    string placeid = data[1];
}
Rex M
Jon's version is funky, but this works and is compatible with non-3.5 stuff.
womp
@womp Jon's version is <strike>funky</strike> awesome, but this works and is compatible with non-3.5 stuff :)
Rex M
So we agree. It's funky awesome.
womp
Just to clarify: my version needs C# 3, but not .NET 3.5. To do it with C# 2 you'd just use an anonymous method instead of a lambda expression.
Jon Skeet
A: 

You could create a private class and place the handler in there. E.g.

    public void downloadphoto(string struri, string strtitle, string placeid)
    {
        using (WebClient wc = new WebClient())
        {
            wcHandler handler = new wcHandler() { Strtitle = strtitle, Placeid = placeid };
            wc.DownloadDataCompleted += handler.wc_DownloadDataCompleted;
            wc.DownloadDataAsync(new Uri(struri));
        }

    }

    private class wcHandler
    {
        public string Strtitle { get; set; }
        public string Placeid { get; set; }

        public void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            // Do Stuff
        }
    }

Although, given the elegance of Jon's answer would probably use that!

JDunkerley