如何避免重复代码以提高效率

发布于 2024-12-01 16:04:47 字数 1619 浏览 0 评论 0原文

我有一个 DataGrid view1 和一个 ListView ,每当我选择列表视图项时(我将 ListView 项传递到查询中并填充根据该项目的 DataGrid 视图)

我已经编写了一些这样的代码......

 private void listview_selectedindexchanged(object sender event args)
 {
     if (listview.SelectedItems.Count > 0 && listview.SelectedItems[0].Group.Name == "abc")
     {
            if(lstview.SelectedItems[0].Text.ToString() == "sfs")
            {
              method1();
            }
            else
            {
                // datagrid view1 binding
               blah.....
             }
     }
     if (lstview.SelectedItems.Count > 0 && lstview.SelectedItems[0].Group.Name == "def")
     {

           if(lstview.SelectedItems[0].Text.ToString() == "xyz")
           {
               method 1();
           }
           if(lstview.SelectedItems[0].Text.ToString() == "ghi")
           {
               method 2(a,b);
           }
           if(lstview.SelectedItems[0].Text.ToString() == "jkl")
           {
               method 2(c,d);
           }
           if(lstview.SelectedItems[0].Text.ToString() == "mno")
           {
               method 3();
           }

       }
   }  
private void method 1()
{ 
  // datagrid view1 binding
    blahh     
}
private void method 2(e,g)
{
  // datagrid view1 binding
  blah....blah..
}
private void method 3()
{

    // datagrid view1 binding
}

我已经像上面那样完成了......我认为这不是一种有效的编码方式。而且这段代码由很多重复的行组成,有什么办法可以将这段代码折射成一小堆代码...... 为了提高效率?

任何提高代码效率的想法和示例片段都会对我有所帮助...

提前非常感谢...

我正在使用 c# 并编写 WinForms 应用程序.....

I have a DataGrid view1 and a ListView and when ever I select the list view item(I am passing the ListView item into the query and populating the DataGrid view according that item)

I have wrote some code like this....

 private void listview_selectedindexchanged(object sender event args)
 {
     if (listview.SelectedItems.Count > 0 && listview.SelectedItems[0].Group.Name == "abc")
     {
            if(lstview.SelectedItems[0].Text.ToString() == "sfs")
            {
              method1();
            }
            else
            {
                // datagrid view1 binding
               blah.....
             }
     }
     if (lstview.SelectedItems.Count > 0 && lstview.SelectedItems[0].Group.Name == "def")
     {

           if(lstview.SelectedItems[0].Text.ToString() == "xyz")
           {
               method 1();
           }
           if(lstview.SelectedItems[0].Text.ToString() == "ghi")
           {
               method 2(a,b);
           }
           if(lstview.SelectedItems[0].Text.ToString() == "jkl")
           {
               method 2(c,d);
           }
           if(lstview.SelectedItems[0].Text.ToString() == "mno")
           {
               method 3();
           }

       }
   }  
private void method 1()
{ 
  // datagrid view1 binding
    blahh     
}
private void method 2(e,g)
{
  // datagrid view1 binding
  blah....blah..
}
private void method 3()
{

    // datagrid view1 binding
}

I have done it like above ... I think this is not an efficient way to do the coding. and this code consisits of a lot of repeated lines, is there any way to refractor this code to a small bunch of code ......
in order improve the efficiency?

Any ideas and sample snippets for increasing code efficiency would be helpful to me ...

Many thanks in advance....

I am using c# and writting WinForms applications.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠我空喜 2024-12-08 16:04:47

您可以将委托保存到列表视图项中。并在选择封装项时调用它。例如,您将像这样填充列表框:

ListViewItem item = new ListViewItem("abc");
item.Tag = new Delegate(method1);
lstview.Items.Add(item);

现在,当选择此项时,您将执行如下方法:

private void listview_selectedindexchanged(object sender event args)
{
    ((Delegate)lstview.SelectedItems[0].Tag)(); // this will execute method1 if the item with text "abc" gets selected
}

注意:!尚未测试此代码,但类似的内容应该可以工作,并且您不必编写 If 语句,您只需正确构造项目即可。

另请注意,对于刚接触此代码的人来说,这可能有点难以阅读。

You could save a delegate into the listview item. And call it when the encapsulating item gets selected. For example you would fill your listbox like this:

ListViewItem item = new ListViewItem("abc");
item.Tag = new Delegate(method1);
lstview.Items.Add(item);

Now, when this item gets selected, you execute the method like so:

private void listview_selectedindexchanged(object sender event args)
{
    ((Delegate)lstview.SelectedItems[0].Tag)(); // this will execute method1 if the item with text "abc" gets selected
}

NOTE: ! have not tested this code, but something along those lines should work and you don't have to write the If-statement, you only have to construct the items correctly.

Also note that this may be a bit hard to read for someone new to this code.

苯莒 2024-12-08 16:04:47

您可以轻松地提取一个新方法来执行“datagrid view1 绑定”。然后从所有需要进行绑定的方法中调用此方法。

You could easily extract a new method to do the "datagrid view1 binding". This method is then called from all the methods that need to do the binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文