简体   繁体   中英

method(Object sender,EventAgrs e) and method ( control c)which parameter type is better?

I have huge confusion between this two type of parameter passing. Both are looks like same. I am send my code here both methods will do the same function. But i don't know which was effective way to programming.

    public void remvuserpro(ListBox lb1)
    {
        string temp1 = "";
        foreach (KeyValuePair<string, int> rm in purchase)
        {

            string temp=rm.Key+"\t"+rm.Value;
            if (temp ==lb1.SelectedItem.ToString())
            {
                temp1 = rm.Key;
               lb1.Items.Remove(lb1.SelectedItem);
               billtotal -= rm.Value;
               label3.Text = Convert.ToString(billtotal);
               break;
            } 
        }

        purchase.Remove(temp1);
    }

    public void rmv(object sender, EventArgs e)
    {
        ListBox lb1 = sender as ListBox;
        string temp1 = "";
        foreach (KeyValuePair<string, int> rm in purchase)
        {
            string temp = rm.Key + "\t" + rm.Value;
            if (temp == lb1.SelectedItem.ToString())
            {
                temp1 = rm.Key;
                lb1.Items.Remove(lb1.SelectedItem);
            }
        }

        purchase.Remove(temp1);
    }

The first one is the correct one in most cases. The second one is for events. In your case, you want the two methods to do the same thing, so you should reuse your own code.

Something like this:

public void rmv(object sender, EventArgs e)
{
    ListBox lb1 = sender as ListBox;
    if(lb1 != null)
        remvuserpro(lb1);
}

In other words, the second method has been autogenerated for you, however, you should not design your own methods like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM