I have this code:
private void ToLocalSiteOnlyToolStripMenuItem_Click(object sender, EventArgs e)
{
if (buttonSwitch == true)
{
ToLocalSiteOnlyToolStripMenuItem.ForeColor = Color.Red;
buttonSwitch = false;
removeExt = true;
}
else
{
ToLocalSiteOnlyToolStripMenuItem.ForeColor = Color.Black;
buttonSwitch = true;
removeExt = false;
}
}
private void removeExternals(List<string> externals)
{
for (int i = 0; i < externals.Count; i++)
{
if (!externals[i].StartsWith(mainUrl))
{
externals.RemoveAt(i);
}
}
}
When i click the menu and its red then im calling the function removeExternals in here:
private List<string> test(string url, int levels,DoWorkEventArgs eve)
{
HtmlWeb hw = new HtmlWeb();
List<string> webSites;
try
{
doc = hw.Load(url);
webSites = getLinks(doc);
removeDupes(webSites);
if (removeExt == true)
{
removeExternals(webSites);
}
Now the webSites List contain links of sites like for example:
www.ynet.co.il
www.hot.co.il
www.walla.co.il
Now the variable mainUrl is http://www.ynet.co.il
I want each iertion im calling the test function and then webSites each time have a different List of links to remove all the links that not start with http://www.ynet.co.il
So in the end each time i will come up in webSites with links that only start with: http://www.ynet.co.il
But it seems that my removeExternals function does not working good as i wanted.
Where is the problem and how to repair it ? Thanks.
The problem is in your loop:
Every time you call
RemoveAt(), you’re changing all the indexes. If you callRemoveAt(1), then everything after 1 is shifted down. I’d recommend looping through in reverse order, like: