开发者

Avoiding code duplication in C# forms

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜