Nov 18, 2010 at 8:47 AM
Edited Nov 18, 2010 at 8:59 AM
EDIT: I realise i could detect if a layer was present by using the GetLayerByName method, but it wouldnt have solved my issue as then Remove or RemoveAt would not have worked.
Last night i came across a minor issue with the IndexOf(ILayer) method in LayerCollection.cs. Slightly unusal issue (perhaps related to the layer itself or how its created)
In short i was trying to replace a layer on a map with a updated version. In order to do this i was looking at the result of IndexOf, if the layer was present (result != -1) then the program would remove that layer. The issue was even
thougth you could see the layer on the map, and the name was present on the list collection the IndexOf value was always -1. I assume that its related to internal object ids etc.
My solution was to Overload the IndexOf method to one that is passed the name of the layer, and returns a index if it comes across a layer with the passed name, -1 if it does not find it. Anyway it seem to do the job for me. Tried to search the forums
for a solution, but nobody seems to have had this problem (or at leasted posted about it). Just thought i'd submit my solution just for future reference. If your intrested in the solution i have created a patch file.
PS. The same could be applied to RemovingALayer. A method like this bool RemoveLayer(string layerName) might be useful. It would just make use of the method below and remove at. Just thinking that may be a simple but perhaps useful for some
feautre. If you think so then i can again code that up and either post the code here or submit a patch. There is no instructions on how to submit a patch on the website so i'm not sure how to do that.
/// Returns the index of the given <paramref name="layerName"/>
/// <param name="layerName">The Name Of The layer</param>
/// <returns>The Index Of The Layer</returns>
public int IndexOf(string layerName)
// Set the index to 0
int index = 0;
// Loop around the collection of layers
foreach (ILayer layer
// if the name of the layer is one we are looking for then
// return the current index.
if (layer.LayerName == layerName)
// increment the index
// This gets returned if the layer is not found by name