This document is archived and information here might be outdated.  Recommended version.


Get FeatureLayer from Layer Index Number Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Get FeatureLayer from Layer Index Number Snippet

Get the FeatureLayer by specifying the Layer Index Number.

[C#]
///<summary>Get the FeatureLayer by specifying the Layer Index Number.</summary>
///
///<param name="activeView">An IActiveView interface</param>
///<param name="layerIndex">An System.Int32 that is the layer index number in the map (zero based). Example: 3</param>
/// 
///<returns>An IFeatureLayer interface (if found) or Nothing (if not found)</returns>
/// 
///<remarks></remarks>
public ESRI.ArcGIS.Carto.IFeatureLayer GetFeatureLayerFromLayerIndexNumber(ESRI.ArcGIS.Carto.IActiveView activeView, System.Int32 layerIndex)
{
  if (activeView == null || layerIndex < 0)
  {
    return null;
  }
  ESRI.ArcGIS.Carto.IMap map=activeView.FocusMap;
  if (layerIndex < map.LayerCount && map.get_Layer(layerIndex) is ESRI.ArcGIS.Carto.IFeatureLayer)
  {
    return (ESRI.ArcGIS.Carto.IFeatureLayer)activeView.FocusMap.get_Layer(layerIndex); // Explicit Cast
  }
  else
  {
    return null;
  }
}
[Visual Basic .NET]
'''<summary>Get the FeatureLayer by specifying the Layer Index Number.</summary>
'''
'''<param name="activeView">An IActiveView interface</param>
'''<param name="layerIndex">An System.Int32 that is the layer index number in the map (zero based). Example: 3</param>
''' 
'''<returns>An IFeatureLayer interface (if found) or Nothing (if not found)</returns>
''' 
'''<remarks></remarks>
Public Function GetFeatureLayerFromLayerIndexNumber(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal layerIndex As System.Int32) As ESRI.ArcGIS.Carto.IFeatureLayer

  If activeView Is Nothing OrElse layerIndex < 0 Then
    Return Nothing
  End If

  Dim map As ESRI.ArcGIS.Carto.IMap=activeView.FocusMap
  If layerIndex < map.LayerCount AndAlso TypeOf map.Layer(layerIndex) Is ESRI.ArcGIS.Carto.IFeatureLayer Then
    Return CType(activeView.FocusMap.Layer(layerIndex), ESRI.ArcGIS.Carto.IFeatureLayer) ' Explicit Cast
  Else
    Return Nothing
  End If

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Carto