I need a piece of code to check if a layer in ArcMap is a FeautureLayer.
IMap pMap = m_mxDoc.FocusMap;
for (int i = 0; i < m_mxDoc.Maps.Count; i++) {
ILayer pLayer = pMap.Layer;
//if (typeof(pMap.Layer) is IFeatureLayer ) ??? How to code here?
Appreciate if you can help.
Here is some code in VBA that shows you how to test if object is of type IFeatureLayer
Public Sub test()
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap
Dim pEnumLayers As IEnumLayer
Set pEnumLayers = pMap.Layers
Dim pLayer As ILayer
Set pLayer = pEnumLayers.Next
Do While Not (pLayer Is Nothing)
If TypeOf pLayer Is IFeatureLayer Then
Debug.Print "FeatureLayer found!"
End If
Set pLayer = pEnumLayers.Next
Loop
End Sub
Revised the code as:
if (m_Map.Layer is IFeatureLayer) { ... }
No need to use typeof(...) Thanks.
You'd be better of enumerating the layers to only get feature layers. For the above code, when you convert it to a .NET equivalent you can provide an ID to the layers command:
Dim pId As New UID
pId.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"
Dim pEnumLayers As IEnumLayer = pMap.Layers(pId, True)
The pId value tells Arc what kind of layers to look for. This page: ArcObjects Help for .NET developers tells you the GUID values for different data types.