避免 C# 表单中的代码重复

发布于 2024-11-14 15:19:17 字数 2683 浏览 2 评论 0原文

我是 C# 的半新手,希望避免代码重复。我有一个父表单和一堆子表单。子表单都包含序列化文本框并将它们发送到公共接口对象的代码:

public partial class Normal : UserControl
    {
    Interfacer normintobj = new Interfacer(STATCTRL.NORMDIST);
    public Normal()
    {
        InitializeComponent();
    }

    private void z_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e,STATMAIN.VINP_Z_NORMAL);
    }

    private void serializethensendinfo()
    {
        normintobj.tbs[0] = UITest.testui(z_tb.Text);
        normintobj.tbs[1] = UITest.testui(Mean_tb.Text);
        normintobj.tbs[2] = UITest.testui(sd_tb.Text);
        normintobj.tbs[3] = UITest.testui(left_tb.Text);
        normintobj.tbs[4] = UITest.testui(tt_tb.Text);
    }

    private void unserializethensendinfo()
    {
        z_tb.Text    = Convert.ToString(normintobj.tbs[0]);
        Mean_tb.Text = Convert.ToString(normintobj.tbs[1]);
        sd_tb.Text = Convert.ToString(normintobj.tbs[2]);
        left_tb.Text = Convert.ToString(normintobj.tbs[3]);
        tt_tb.Text = Convert.ToString(normintobj.tbs[4]);
    }

    private void sendinfo(KeyEventArgs e,int field)
    {
        serializethensendinfo();
        normintobj.chk_tb_type(ref textBlock1, field, e);
        unserializethensendinfo();
    }

    private void sendinfo(int field)
    {
        serializethensendinfo();
        normintobj.chk_tb_type(ref textBlock1, field);
        unserializethensendinfo();
    }

    private void Mean_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e,STATMAIN.NORMDIST_MID);
    }

    private void sd_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e,STATMAIN.NORMDIST_MID);
    }

    private void left_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e, STATMAIN.VOUT_LEFT_NORMAL);
    }

    private void tt_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e,STATMAIN.VOUT_LEFT_NORMAL);
    }


    private void z_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo( STATMAIN.VINP_Z_NORMAL);
    }

    private void Mean_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo(STATMAIN.NORMDIST_MID);
    }

    private void sd_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo(STATMAIN.NORMDIST_MID);
    }

    private void left_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo(STATMAIN.VOUT_LEFT_NORMAL);
    }

    private void tt_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo(STATMAIN.VOUT_TWO_NORMAL);
    }

我对serializethensendinfo、unserializeandsendinfo和重载的sendinfo方法有一个真正的问题...我发现自己在所有子表单上剪切和粘贴相同的函数(大约 20 个),然后更改文本框的名称。当文本框的名称都不同时,如何避免代码重复?

I'm a semi-newbie to C# and am looking to avoid code duplication. I have a parent form and a bunch of subforms. The subforms all contain code that serializes the textboxes and sends them to a common interfacer object:

public partial class Normal : UserControl
    {
    Interfacer normintobj = new Interfacer(STATCTRL.NORMDIST);
    public Normal()
    {
        InitializeComponent();
    }

    private void z_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e,STATMAIN.VINP_Z_NORMAL);
    }

    private void serializethensendinfo()
    {
        normintobj.tbs[0] = UITest.testui(z_tb.Text);
        normintobj.tbs[1] = UITest.testui(Mean_tb.Text);
        normintobj.tbs[2] = UITest.testui(sd_tb.Text);
        normintobj.tbs[3] = UITest.testui(left_tb.Text);
        normintobj.tbs[4] = UITest.testui(tt_tb.Text);
    }

    private void unserializethensendinfo()
    {
        z_tb.Text    = Convert.ToString(normintobj.tbs[0]);
        Mean_tb.Text = Convert.ToString(normintobj.tbs[1]);
        sd_tb.Text = Convert.ToString(normintobj.tbs[2]);
        left_tb.Text = Convert.ToString(normintobj.tbs[3]);
        tt_tb.Text = Convert.ToString(normintobj.tbs[4]);
    }

    private void sendinfo(KeyEventArgs e,int field)
    {
        serializethensendinfo();
        normintobj.chk_tb_type(ref textBlock1, field, e);
        unserializethensendinfo();
    }

    private void sendinfo(int field)
    {
        serializethensendinfo();
        normintobj.chk_tb_type(ref textBlock1, field);
        unserializethensendinfo();
    }

    private void Mean_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e,STATMAIN.NORMDIST_MID);
    }

    private void sd_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e,STATMAIN.NORMDIST_MID);
    }

    private void left_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e, STATMAIN.VOUT_LEFT_NORMAL);
    }

    private void tt_tb_KeyDown(object sender, KeyEventArgs e)
    {
        sendinfo(e,STATMAIN.VOUT_LEFT_NORMAL);
    }


    private void z_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo( STATMAIN.VINP_Z_NORMAL);
    }

    private void Mean_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo(STATMAIN.NORMDIST_MID);
    }

    private void sd_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo(STATMAIN.NORMDIST_MID);
    }

    private void left_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo(STATMAIN.VOUT_LEFT_NORMAL);
    }

    private void tt_tb_LostFocus(object sender, RoutedEventArgs e)
    {
        sendinfo(STATMAIN.VOUT_TWO_NORMAL);
    }

I have a real problem with the serializethensendinfo, unserializeandsendinfo, and the overloaded sendinfo methods... I find myself cutting and pasting the same functions on all my subforms (about 20 of them), but then changing the names of the textboxes. How do you avoid code duplication when the textboxes are all different names?

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

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

发布评论

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

评论(1

岁月流歌 2024-11-21 15:19:17

如果不规范所有页面上的文本框,然后将这些方法推入基类,则没有简单的修复方法。

您可以执行一些操作,例如将方法推入基类并更改方法签名以获取列表,然后在页面级别使用将创建列表的方法。

它应该减少逻辑重复,但您仍然需要编写一些代码。

private void serializethensendinfo( IEnumberable<TextBox> listOfTextBoxes )
{
    for( int i = 0; i < listOfTextBoxes.Count; i++ )
    {
       normintobj.tbs[i] = UITest.testui(listOfTextBoxes[i].Text);
    }    
}

private void unserializethensendinfo( IEnumberable<TextBox> listOfTextBoxes )
{
    for( int i = 0; i < listOfTextBoxes.Count; i++ )
    {
       listOfTextBoxes[i].Text = Convert.ToString(normintobj.tbs[i]);

       normintobj.tbs[i] = UITest.testui(listOfTextBoxes[i].Text);
    } 
}

此代码未经测试,您可能需要传入 List byref,以便可以写回文本框。

另外,该列表可能需要在页面级别静态定义...如果没有进行一些测试并完全理解方法的用例,我不能 100% 确定。

Without normalizing the text boxes on all the pages and then pushing those methods up into a base class, there is no easy fix.

You could do something like pushing the methods up into a base class and change the method signatures to take an List then having methods at the page level that will create the List.

It should cut down on the logic duplication but you will still have some code to write.

private void serializethensendinfo( IEnumberable<TextBox> listOfTextBoxes )
{
    for( int i = 0; i < listOfTextBoxes.Count; i++ )
    {
       normintobj.tbs[i] = UITest.testui(listOfTextBoxes[i].Text);
    }    
}

private void unserializethensendinfo( IEnumberable<TextBox> listOfTextBoxes )
{
    for( int i = 0; i < listOfTextBoxes.Count; i++ )
    {
       listOfTextBoxes[i].Text = Convert.ToString(normintobj.tbs[i]);

       normintobj.tbs[i] = UITest.testui(listOfTextBoxes[i].Text);
    } 
}

This code is untested and you might need to pass in the List byref so that you can write back to the text boxes.

Also, that list might need to be staticly defined at the page level... without doing some testing and fully understanding the use case of the methods, I'm not 100% sure.

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