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 cleanCombo1;
String cleanCombo2;
String cleanCombo3;
String cleanCombo4;
String cleanCombo;
public string GetTextBetween(string firstPart, string secondPart, string lastPart)
{
String St1 = plHTMLP1.Text;
int pFrom1 = St1.IndexOf(firstPart) + firstPart.Length;
int pTo1 = St1.IndexOf(lastPart, pFrom1);
if (St1.Substring(pFrom1, pTo1 - pFrom1).Contains(secondPart))
{
cleanCombo1 = St1.Substring(pFrom1, pTo1 - pFrom1);
}
String St2 = plHTMLP2.Text;
int pFrom2 = St2.IndexOf(firstPart) + firstPart.Length;
int pTo2 = St2.IndexOf(lastPart, pFrom2);
if (St2.Substring(pFrom2, pTo2 - pFrom2).Contains(secondPart))
{
cleanCombo2 = St2.Substring(pFrom2, pTo2 - pFrom2);
}
String St3 = plHTMLP3.Text;
int pFrom3 = St3.IndexOf(firstPart) + firstPart.Length;
int pTo3 = St3.IndexOf(lastPart, pFrom3);
if (St3.Substring(pFrom3, pTo3 - pFrom3).Contains(secondPart))
{
cleanCombo3 = St3.Substring(pFrom3, pTo3 - pFrom3);
}
String St4 = plHTMLP4.Text;
int pFrom4 = St4.IndexOf(firstPart) + firstPart.Length;
int pTo4 = St4.IndexOf(lastPart, pFrom4);
if (St4.Substring(pFrom4, pTo4 - pFrom4).Contains(secondPart))
{
cleanCombo4 = St4.Substring(pFrom4, pTo4 - pFrom4);
}
cleanCombo = cleanCombo1 + cleanCombo2 + cleanCombo3 + cleanCombo4;
return cleanCombo;
}
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)
{
String firstPart = "<strong>http://m2.";
String secondPart = "m.com</strong>";
String lastPart = "</p>";
GetTextBetween(firstPart, secondPart, lastPart);
plHTML.Text = FilterHTML(cleanCombo, "m.com");
}
Not sure if this other Method I made affects the code but here it is
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;
}
I think there is a problem with the scope of the variables cleanCombo1
, cleanCombo2
, cleanCombo3
, cleanCombo4
. These should be declared within the scope of the method GetTextBetween
. Your newer method also use these variables and if GetTextBetween
does not enter any of the if
blocks, it will reuse garbage values inside the cleanComboX
variables.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.