What's the best way to have repeat code in C#?

点点圈 提交于 2019-12-10 12:26:30

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/30638197/whats-the-best-way-to-have-repeat-code-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!