[GIS] Finding TOC Layers Type (layer,grouplayer,..) and layerindex of a layer

arcobjects

How can i find the type of the layers in TOC ?
these types are very important to me :

1.layer (that has a valid datasource)

2.layer (that lose its datasource because of changing the address of its datasource )

3.GroupLayer

And i want to know about the index of the layer that we have with ILayer. A function like this :
Public Function GetLayerIndex(mLayer as ILayer) as long

Best Answer

You examine the interfaces which a particular layer implements. All layers implement ILayer. If you open up the documentation link for the interface, you will see which classes implement this general interface.

Common layer types (in context of your question) are FeatureLayer (implements IFeatureLayer among others), GroupLayer (ICompositeLayer, IGroupLayer) etc. You get the idea - when exploring the plethora of interfaces layers can implement, it's good to refer to the list of classes implementing ILayer as a great starting point.

As for the second part, layers which are not connected due to an inaccessible data source will typically be marked as invalid (red exclamation mark). The ILayer.Valid indicates whether this is or is not the case.

Finally, the third part of your question. Layer indices are assigned either at the top level (the index which goes as a parameter to IMap.Layer) or within a particular layer container, e. g. a composite (group) layer. The only way to get retrieve the index for an existing layer is to loop through the map or a composite layer and compare the items. This will work only at a single level and will not work reliably when layers are nested with group layers. That being said, layer indices are generally not a good idea to refer to layers.