This project has moved and is read-only. For the latest updates, please go here.

Slight Issue With LayerCollection.IndexOf

Topics: Data Access, SharpMap v2.0
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.
Morning all.

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. 


/// <summary>
/// Returns the index of the given <paramref name="layerName"/>
/// </summary>
/// <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 in List)
           // if the name of the layer is one we are looking for then
           // return the current index.
           if (layer.LayerName == layerName)
               return index;

           // increment the index

     // This gets returned if the layer is not found by name
     return -1;






Nov 18, 2010 at 10:05 AM

Hello Edmund0dantes,

I hesitate to apply that since I don't know if it breaks existing code, why don't you go for a

/// Removes the layer with the provided name if present
///<param name="layerName">The name of the layer</param>
public void Remove(string layerName)

/// Replace the layer which has the same name as the 
/// provided new layer with the new layer. 
/// If no layer with this criteria exist, the new layer will be added.
///<param name="layer">The layer to replace or add</param>
public void Replace(ILayer layer)

/// Searches the collection for a layer with the given name
/// If no layer is found returns null
///<param name="layerName">The name of the layer to look for</param>
public ILayer Find(string layerName)


Hth FObermaier

Nov 18, 2010 at 10:20 AM

Thanks for your response

I like the idea of the Replace function - That would be a lot cleaner than my exisitng solution. I've done Remove now, replace would proably use that functionality as well.

Thanks Again.