C# 变量正在从我的C语言方法中泄漏#
我现在有一个代码块,在我的程序中除了几个变量外,在按钮点击中重复。问题是,当我从单击一个按钮转到另一个按钮时,C# 变量正在从我的C语言方法中泄漏#,c#,methods,C#,Methods,我现在有一个代码块,在我的程序中除了几个变量外,在按钮点击中重复。问题是,当我从单击一个按钮转到另一个按钮时,plHTML开始从上一个按钮泄漏文本。在我尝试使用这种方法之前,这种情况并没有发生 如何防止变量相互泄漏 这是我尝试过的方法 String cleanCombo1; String cleanCombo2; String cleanCombo3; String cleanCombo4; String cleanCombo; public st
plHTML
开始从上一个按钮泄漏文本。在我尝试使用这种方法之前,这种情况并没有发生
如何防止变量相互泄漏
这是我尝试过的方法
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;
}
我有三个按钮(目前)使用这种方法
下面是其中一个按钮包含的代码示例
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");
}
private void mButton\u单击(对象发送者,事件参数e)
{
String firstPart=“http://m2.";
字符串secondPart=“m.com”;
字符串lastPart=“”;
GetTextBetween(第一部分、第二部分、最后一部分);
Text=filtertHTML(cleanCombo,“m.com”);
}
我不确定我所做的其他方法是否会影响代码,但这里就是
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;
}
publicstringfilterthtml(stringcleancombo,stringendofstring)
{
cleanCombo=cleanCombo.Replace(“”,”);
cleanCombo=cleanCombo.Replace(“
,”);
cleanCombo=cleanCombo.Replace(“”,”);
cleanCombo=cleanCombo.Replace(EndOfString,“”);
返回cleanCombo;
}
我认为变量cleanCombo1
,cleanCombo2
,cleanCombo3
,cleanCombo4
的范围有问题。这些应该在方法GetTextBetween
的范围内声明。您较新的方法也使用这些变量,如果GetTextBetween
没有输入任何if
块,它将重用cleanComboX
变量中的垃圾值。可能重复的