2

以下方法只是彼此的倒数。我怀疑我可以将逻辑组合成一种方法。我更喜欢避免反射。是否可以将它们结合起来并保持可读性?

    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
    }
4

7 回答 7

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);
}

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

于 2012-06-06T18:29:26.603 回答
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);
}
于 2012-06-06T18:31:22.247 回答
1

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

于 2012-06-06T18:32:53.840 回答
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
}
}

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

于 2012-06-06T18:29:10.923 回答
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);
        }
    }
于 2012-06-06T18:31:48.253 回答
0

每个人都没有过火而牺牲代码的可读性是正确的

我唯一的想法是...

仅从发布的代码来看,它看起来像selectedLabsData并且availableLabsData紧密交织在一起,因此我不会将与它们相关的逻辑放在您的事件处理程序中。将逻辑放在不同的方法中(最好是不同的类),这样您就不会不小心更新一个而不更新另一个。这样做的好处是使所讨论的方法更简单、更“可读”。

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);
于 2012-06-06T19:00:29.377 回答
-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);
}
于 2012-06-06T18:23:51.257 回答