什么是有重复的代码在C#中的最佳方式? [英] What's the best way to have repeat code in C#?

查看:191
本文介绍了什么是有重复的代码在C#中的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有重复我的整个程序按钮点击里面的几个变量的异常代码块。问题是,当我从点击一个按钮到另一个按钮去, plHTML 开始泄漏从以前的按钮上的文字。之前,我试图使该方法这并没有发生。



我怎样才能防止变量泄漏到对方?



这是我试图让

 字符串firstPart方法; 
字符串lastPart;
串二部;
字符串cleanCombo1,cleanCombo2,cleanCombo3,cleanCombo4;

公共无效GetTextBetween(串一,二串,串尾)
{
字符串ST1 = passlotHTMLP1.Text;
INT pFrom1 = St1.IndexOf(第一)+ First.Length;
INT PTO1 = St1.IndexOf(最后,pFrom1);
如果(St1.Substring(pFrom1,PTO1 - pFrom1)。载(二))
{
cleanCombo1 = St1.Substring(pFrom1,PTO1 - pFrom1);
}
字符串ST2 = passlotHTMLP2.Text;
INT pFrom2 = St2.IndexOf(第一)+ First.Length;
INT PTO2 = St2.IndexOf(最后,pFrom2);
如果(St2.Substring(pFrom2,PTO2 - pFrom2)。载(二))
{
cleanCombo2 = St2.Substring(pFrom2,PTO2 - pFrom2);
}
St3中的String = passlotHTMLP3.Text;
INT pFrom3 = St3.IndexOf(第一)+ First.Length;
INT pTo3 = St3.IndexOf(最后,pFrom3);
如果(St3.Substring(pFrom3,pTo3 - pFrom3)。载(二))
{
cleanCombo3 = St3.Substring(pFrom3,pTo3 - pFrom3);
}
字符串St4中= passlotHTMLP4.Text;
INT pFrom4 = St4.IndexOf(第一)+ First.Length;
INT pTo4 = St4.IndexOf(最后,pFrom4);
如果(St4.Substring(pFrom4,pTo4 - pFrom4)。载(二))
{
cleanCombo4 = St4.Substring(pFrom4,pTo4 - pFrom4);
}
}

和我有三个按钮(目前)的使用这种方法。



下面是代码的其中一个按钮包含

的例子私人无效mButton_Click(对象发件人,EventArgs五)
{
pLink.Text =http://www.m.com;
plHTML.Visible = FALSE;

{
firstPart =<强>的http://平方米;
二部=m.com< / STRONG>中;
lastPart =下; / P>中;
GetTextBetween(firstPart,二部,lastPart);
cleanCombo = cleanCombo1 + cleanCombo2 + cleanCombo3 + cleanCombo4;
//MessageBox.Show(cleanCombo);
FilterHTML(m.com);
//MessageBox.Show(cleanCombo);
plHTML.Text = cleanCombo;
随机RND =新的随机();
是getAccount:

{
INT randomInt = rnd.Next(1,15);
如果
{
plHTML.Text = pltHTML.Lines [randomInt](plHTML.Lines [randomInt]。载有(\)!);
plHTML.Visible = TRUE;
}
,否则
{
转到是getAccount;
}
}
赶上(例外)
{
转到是getAccount;
}
}
赶上(例外)
{
plHTML.Visible = TRUE;
plHTML.Text =没有可用的帐户;
}
}

不知道如果我做了这个其他方法影响的代码,但在这里它是

 字符串cleanCombo; 

公共无效FilterHTML(字符串EndOfString)
{
cleanCombo = cleanCombo.Replace(<强>,);
cleanCombo = cleanCombo.Replace(< BR />中,);
cleanCombo = cleanCombo.Replace(< / STRONG>中,);
cleanCombo = cleanCombo.Replace(EndOfString,);
}


解决方案

我怎样才能防止变量泄漏到彼此




变量范围:的 http://en.wikipedia.org/wiki/Scope_%28computer_science%29



基本上,确保你的方法是自包含的 - 即你想要什么,并传递出去想你想要的。

 公共字符串FilterHTML(字符串cleanCombo,串EndOfString)
{
cleanCombo = cleanCombo.Replace( <强>,);
cleanCombo = cleanCombo.Replace(&所述峰; br />中,);
cleanCombo = cleanCombo.Replace(< / STRONG>中,);
cleanCombo = cleanCombo.Replace(EndOfString,);

返回cleanCombo;
}

plHTML.Text = FilterHTML(cleanCombo1 + cleanCombo2 + cleanCombo3 + cleanCombo4,m.com);



这同样适用于该按钮事件,在通过按钮作为一个参数,并工作在其上共享事件。


I have currently have a block of code that is repeated throughout my program inside Button Clicks with the exception of a few variables. The issue is that when I go from clicking one button to another button, plHTML starts leaking text from the previous button. This did not happen before I tried making the method.

How can I prevent the variables from leaking into each other?

This is the method I have attempted to make

    String firstPart;
    String lastPart;
    String secondPart;
    String cleanCombo1, cleanCombo2, cleanCombo3, cleanCombo4;

    public void GetTextBetween(string First, string Second, string Last)
    {
        String St1 = passlotHTMLP1.Text;
        int pFrom1 = St1.IndexOf(First) + First.Length;
        int pTo1 = St1.IndexOf(Last, pFrom1);
        if (St1.Substring(pFrom1, pTo1 - pFrom1).Contains(Second))
        {
            cleanCombo1 = St1.Substring(pFrom1, pTo1 - pFrom1);
        }
        String St2 = passlotHTMLP2.Text;
        int pFrom2 = St2.IndexOf(First) + First.Length;
        int pTo2 = St2.IndexOf(Last, pFrom2);
        if (St2.Substring(pFrom2, pTo2 - pFrom2).Contains(Second))
        {
            cleanCombo2 = St2.Substring(pFrom2, pTo2 - pFrom2);
        }
        String St3 = passlotHTMLP3.Text;
        int pFrom3 = St3.IndexOf(First) + First.Length;
        int pTo3 = St3.IndexOf(Last, pFrom3);
        if (St3.Substring(pFrom3, pTo3 - pFrom3).Contains(Second))
        {
            cleanCombo3 = St3.Substring(pFrom3, pTo3 - pFrom3);
        }
        String St4 = passlotHTMLP4.Text;
        int pFrom4 = St4.IndexOf(First) + First.Length;
        int pTo4 = St4.IndexOf(Last, pFrom4);
        if (St4.Substring(pFrom4, pTo4 - pFrom4).Contains(Second))
        {
            cleanCombo4 = St4.Substring(pFrom4, pTo4 - pFrom4);
        }
    }

And I have three buttons (at the moment) that use this Method.

Here is an example of the code that one of the buttons contain

private void mButton_Click(object sender, EventArgs e)
{
    pLink.Text = "http://www.m.com";
    plHTML.Visible = false;
    try
    {
        firstPart = "<strong>http://m2.";
        secondPart = "m.com</strong>";
        lastPart = "</p>";
        GetTextBetween(firstPart, secondPart, lastPart);
        cleanCombo = cleanCombo1 + cleanCombo2 + cleanCombo3 + cleanCombo4;
        //MessageBox.Show(cleanCombo);
        FilterHTML("m.com");
        //MessageBox.Show(cleanCombo);
        plHTML.Text = cleanCombo;
        Random rnd = new Random();
    getAccount:
        try
        {
            int randomInt = rnd.Next(1, 15);
            if (!plHTML.Lines[randomInt].Contains("\""))
            {
                plHTML.Text = pltHTML.Lines[randomInt];
                plHTML.Visible = true;
            }
            else
            {
                goto getAccount;
            }
        }
        catch (Exception)
        {
            goto getAccount;
        }
    }
    catch (Exception)
    {
        plHTML.Visible = true;
        plHTML.Text = "No accounts available";
    }
}

Not sure if this other Method I made affects the code but here it is

String cleanCombo;

public void FilterHTML(string EndOfString)
{
    cleanCombo = cleanCombo.Replace("<strong>", "");
    cleanCombo = cleanCombo.Replace("<br />", "");
    cleanCombo = cleanCombo.Replace("</strong>", "");
    cleanCombo = cleanCombo.Replace(EndOfString, "");
}

解决方案

How can I prevent the variables from leaking into each other?

Variable scope: http://en.wikipedia.org/wiki/Scope_%28computer_science%29

Basically, make sure your methods are self-contained - ie you pass in what you want and get out want you want.

public string FilterHTML(string cleanCombo, string EndOfString)
{
    cleanCombo = cleanCombo.Replace("<strong>", "");
    cleanCombo = cleanCombo.Replace("<br />", "");
    cleanCombo = cleanCombo.Replace("</strong>", "");
    cleanCombo = cleanCombo.Replace(EndOfString, "");

    return cleanCombo;
}

plHTML.Text = FilterHTML(cleanCombo1 + cleanCombo2 + cleanCombo3 + cleanCombo4, "m.com");

The same applies for the button event, pass the button in as a parameter and work on it in the shared event.

这篇关于什么是有重复的代码在C#中的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆