Get GeoFeatureLayer from Layer Index Number Snippet (ArcObjects .NET 10.6 SDK)
ArcObjects Library Reference

Get GeoFeatureLayer from Layer Index Number Snippet

Get the GeoFeatureLayer by specifying the Layer Index Number.

[C#]
///<summary>Get the GeoFeatureLayer 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: 1</param>
/// 
///<returns>An IGeoFeatureLayer interface (if found) or Nothing (if not found)</returns>
/// 
///<remarks></remarks>
public ESRI.ArcGIS.Carto.IGeoFeatureLayer GetGeoFeatureLayerFromLayerIndexNumber(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.IGeoFeatureLayer)
  {
    return (ESRI.ArcGIS.Carto.IGeoFeatureLayer)activeView.FocusMap.get_Layer(layerIndex); // Explicit Cast
  }
  else
  {
    return null;
  }
}
[Visual Basic .NET]
'''<summary>Get the GeoFeatureLayer 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: 1</param>
''' 
'''<returns>An IGeoFeatureLayer interface (if found) or Nothing (if not found)</returns>
''' 
'''<remarks></remarks>
Public Function GetGeoFeatureLayerFromLayerIndexNumber(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal layerIndex As System.Int32) As ESRI.ArcGIS.Carto.IGeoFeatureLayer

  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.IGeoFeatureLayer Then

    Return CType(activeView.FocusMap.Layer(layerIndex), ESRI.ArcGIS.Carto.IGeoFeatureLayer) ' 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