如何将这两种相似的方法合二为一?

时间:2012-06-06 18:21:24

标签: c#

以下方法只是彼此的反转。我怀疑我可以将逻辑组合成一种方法。我宁愿避免反思。是否可以将它们结合起来并保持可读性?

    private void btnAdd_Click(object sender, EventArgs e)
    {
        LabEntity selectedItem = bindingSource1.Current as LabEntity;
        selectedLabsData.Add(selectedItem);
        availableLabsData.Remove(selectedItem);
    }

    private void btnRemove_Click(object sender, EventArgs e)
    {
        LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
        availableLabsData.Add(selectedItem);//called Add instead of remove
        selectedLabsData.Remove(selectedItem);//called Remove instead of Add
    }

7 个答案:

答案 0 :(得分:13)

您可以将逻辑分解为辅助方法:

private void ListFixup(object entity, List<Item> addList, List<Item> removeList)
{
    LabEntity selectedItem = entity as LabEntity;
    // don't forget your error checking here

    addList.Add(selectedItem);
    removeList.Remove(selectedItem);
}

private void btnAdd_Click(object sender, EventArgs e)
{
    ListFixup(bindingSource1.Current, selectedLabsData, availableLabsData);
}

private void btnRemove_Click(object sender, EventArgs e)
{
    ListFixup(bindingSource2.Current, availableLabsData, selectedLabsData);
}

我不确定这有助于提高可读性,但它确实减少了代码重复。

答案 1 :(得分:1)

private void btnAdd_Click(object sender, EventArgs e)
{
   SwapThem( bindingSource1, selectedLabsData, availableLabsData );
}

private void btnRemove_Click(object sender, EventArgs e)
{
   SwapThem( bindingSource2, availableLabsData, selectedLabsData );
}

// I just don't know the proper type-cast of the "toAddTo" and "toRemoveFrom" parameters.
private void SwapThem( BindingSource bs, List<yourType> toAddTo, List<yourType> toRemoveFrom )
{
   LabEntity selectedItem = bs.Current as LabEntity;
   toAddTo.Add(selectedItem);
   toRemoveFrom.Remove(selectedItem);
}

答案 2 :(得分:1)

没有任何方法可以重构两种方法,这些方法不会显着降低代码的可读性,您可以在发布的其他一些答案中看到这一点。为了便于阅读,这种情况下代码重复的级别是可接受的。

答案 3 :(得分:0)

为发件人添加标签?

private void btnClick(object sender, EventArgs e)
{
Button *myButton = (Button)sender;
if (myButton.tag == 1){
        LabEntity selectedItem = bindingSource1.Current as LabEntity;
        selectedLabsData.Add(selectedItem);
        availableLabsData.Remove(selectedItem);
}
else {
LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
        availableLabsData.Add(selectedItem);//called Add instead of remove
        selectedLabsData.Remove(selectedItem);//called Remove instead of Add
}
}

我没有检查过这个编译错误,这只是一个例子。

答案 4 :(得分:0)

将两个按钮链接到单个事件处理程序。处理程序可能看起来像这样:(原谅对按钮的多次检查,因为我不知道要为'availableLabsData'和'selectedLabsData'声明持有者的类型):

    private void btnClick(object sender, EventArgs e)
    {
        var bindingSource = (sender == btnRemove) ? bindingSource2 : bindingSource1;   
        var selectedItem = source.Current as LabEntity;
        if(sender == btnRemove) 
        {
            availableLabsData.Add(selectedItem);
            selectedLabsData.Remove(selectedItem);
        }
        else if(sender == btnAdd)
        {
            availableLabsData.Remove(selectedItem);
            selectedLabsData.Add(selectedItem);
        }
    }

答案 5 :(得分:0)

每个人都不应该以牺牲代码的可读性为代价而过度使用

我唯一的想法是......

从发布的代码开始,但看起来selectedLabsDataavailableLabsData紧密交织在一起,所以我不会在事件处理程序中放置与它们相关的逻辑。将逻辑放在不同的方法中(最好是不同的类),这样你就不会在不更新另一个的情况下意外更新逻辑。这样做的好处是可以使所讨论的方法更简单,更“可读”。

private void LabsDataAdded(LabEntity value)
{
    selectedLabsData.Add(value);
    availableLabsData.Remove(value);
}

private void LabsDataRemoved(LabEntity value)
{
    availableLabsData.Add(value);
    selectedLabsData.Remove(value);
}

然后方法就是:

private void btnAdd_Click(object sender, EventArgs e)
{
    LabsDataAdded(bindingSource1.Current as LabEntity);
}

private void btnRemove_Click(object sender, EventArgs e)
{
    LabsDataRemoved(bindingSource2.Current as LabEntity);
}

更好的是,您可以使用lambda表达式使代码更简洁:

btnAdd.Clicked += (sender, e) => LabsDataAdded(bindingSource1.Current as LabEntity);
btnAdd.Clicked += (sender, e) => LabsDataRemoved(bindingSource1.Current as LabEntity);

答案 6 :(得分:-2)

这样的事情:

private void btnAdd_Click(object sender, EventArgs e)
{
    LabEntity selectedItem = bindingSource1.Current as LabEntity;
    RemoveItemFromList(selectedItem);
}

private void btnRemove_Click(object sender, EventArgs e)
{
    LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
    RemoveItemFromList(selectedItem);
}

private void RemoveItemFromList(LabEntity ent)
{
    selectedLabsData.Add(ent);
    availableLabsData.Remove(ent);
}