Quantcast
Channel: DotSpatial
Viewing all articles
Browse latest Browse all 3973

New Post: Removing a leyer by code

$
0
0
Hey am2,

as far as I can see you're using the correct methode to remove the layer. But you should never use for each if you want to remove something from a list.
Do it this way:
for (var i = map.Layers.Count-1; i >= 0; i--)
{
  if (map.Layers[i].DataSet != null && map.Layers[i].DataSet.Name == "USE2000Layer")
      map.Layers[i].IsVisible = false;
  map.Layers.Remove(map.Layers[i]);
}
Foreach relies on an internal counter with a certain length. If you remove an item and go to the next, the list is one item shorter than the counter expects and you get an error. If you remove things from the end of the list it's all the same whether the list became shorter because all the items at the beginning are still there and thats where your going.

jany_

Viewing all articles
Browse latest Browse all 3973

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>