12

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));
    }
}
i_am_jorf
  • 51,120
  • 15
  • 123
  • 214
dezkev
  • 569
  • 2
  • 7
  • 14

4 Answers4

25

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
  • 1,261,211
  • 792
  • 8,724
  • 8,929
5

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
  • 135,205
  • 29
  • 270
  • 310
2

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
  • 11,869
  • 5
  • 39
  • 45
1

Jon Skeet already answered this, showing how to use a lamda expression, but I was still unclear about it. I still needed some more examples, and eventually found this simple case using a button: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/74d03fe0-0fa5-438d-80e0-cf54fa15af0e

void A()  
{  
  Popup parameter = new Popup();  
  buttonClose.Click += (sender, e) => { buttonClose_Click(sender, e, parameter); };  
}  

static void buttonClose_Click(object sender, EventArgs e, Popup parameter)     
{     
  MakeSomethingWithPopupParameter(parameter);  
}


In my case, I was using a context menu for a TreeView control, which ended up looking like this:

private void TreeViewCreateContextMenu(TreeNode node)
{
    ContextMenuStrip contextMenu = new ContextMenuStrip();

    // create the menu items
    ToolStripMenuItem newMenuItem = new ToolStripMenuItem();
    newMenuItem.Text = "New...";

    // add the menu items to the menu
    contextMenu.Items.AddRange(new ToolStripMenuItem[] { newMenuItem });

    // add its event handler using a lambda expression, passing
    //  the additional parameter "myData"
    string myData = "This is the extra parameter.";
    newMenuItem.Click += (sender, e) => { newMenuItem_Click(sender, e, myData); };

    // finally, set the node's context menu
    node.ContextMenuStrip = contextMenu;
}

// the custom event handler, with "extraData":
private void newMenuItem_Click(object sender, EventArgs e, string extraData)
{
    // do something with "extraData"
}
Alan
  • 3,555
  • 3
  • 35
  • 54