在C#中重复代码的最佳方法是什么?

时间:2015-06-04 08:01:28

标签: c# methods

我目前有一个代码块在Button Clicks中的整个程序中重复,但有一些变量除外。问题是,当我从单击一个按钮转到另一个按钮时,plHTML开始从前一个按钮泄漏文本。在我尝试制作该方法之前,这种情况并没有发生。

如何防止变量相互泄漏?

这是我试图制作的方法

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

我有三个按钮(目前)使用此方法。

以下是其中一个按钮包含

的代码示例
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";
    }
}

不确定我制作的其他方法是否会影响代码,但此处是

String cleanCombo;

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

1 个答案:

答案 0 :(得分:0)

  

如何防止变量相互泄漏?

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

基本上,确保你的方法是自包含的 - 即你传递你想要的东西并想要你想要的东西。

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

同样适用于按钮事件,将按钮作为参数传递,并在共享事件中处理它。