ArcObjects Library Reference

Get FeatureClass of Selected Feature Layer in Contents View Snippet

Returns a reference to the currently selected featureclass from the given contents view.

[C#]

///<summary>Returns a reference to the currently selected featureclass from the given contents view.</summary>
///  
///<param name="currentContentsView">An IContentsView interface.</param>
///  
///<returns>An IFeatureClass interface or Nothing if not found.</returns>
///   
///<remarks></remarks>
public ESRI.ArcGIS.Geodatabase.IFeatureClass GetFeatureClassOfSelectedFeatureLayerInContentsView(ESRI.ArcGIS.ArcMapUI.IContentsView currentContentsView)
{
  if (currentContentsView == null)
  {
    return null;
  }
  if (currentContentsView.SelectedItem is ESRI.ArcGIS.Carto.IFeatureLayer)
  {
    ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)currentContentsView.SelectedItem; // Explicit Cast
    ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureLayer.FeatureClass;

    return featureClass;
  }
  return null;
}
[Visual Basic .NET]

'''<summary>Returns a reference to the currently selected featureclass from the given contents view.</summary>
'''  
'''<param name="currentContentsView">An IContentsView interface.</param>
'''  
'''<returns>An IFeatureClass interface or Nothing if not found.</returns>
'''   
'''<remarks></remarks>
Public Function GetFeatureClassOfSelectedFeatureLayerInContentsView(ByVal currentContentsView As ESRI.ArcGIS.ArcMapUI.IContentsView) As ESRI.ArcGIS.Geodatabase.IFeatureClass

  If currentContentsView Is Nothing Then
    Return Nothing
  End If

  If TypeOf currentContentsView.SelectedItem Is ESRI.ArcGIS.Carto.IFeatureLayer Then
    Dim featureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = CType(currentContentsView.SelectedItem, ESRI.ArcGIS.Carto.IFeatureLayer) ' Explicit Cast
    Dim featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = featureLayer.FeatureClass

    Return featureClass

  End If

  Return Nothing

End Function


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