Variables are leaking from my methods in C# -
i have have block of code repeated throughout program inside button clicks exception of few variables. issue when go clicking 1 button button, plhtml
starts leaking text previous button. did not happen before tried making method.
how can prevent variables leaking each other?
this method have attempted 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 have 3 buttons (at moment) use method.
here example of code 1 of 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 other method made affects code here 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 problem scope of variables cleancombo1
, cleancombo2
, cleancombo3
, cleancombo4
. these should declared within scope of method gettextbetween
. newer method use these variables , if gettextbetween
not enter of if
blocks, reuse garbage values inside cleancombox
variables.
Comments
Post a Comment