Get attribute value from selected feature

4528
4
11-25-2011 07:26 AM
BrianWestfall
New Contributor
I am new to using arcobjects and need to do something that should be very simple.

Assuming a user has selected a point feature, I simply need to get a specific attribute value from a known field.

For example, a user selects a USGS gage that is represented as a point.  I need to get the gage ID from the Gage_ID field in the attribute table to do some additional processing. I am using Visual Studio 2010 and vb.net.

Any help would be appreciated.

Thanks

Brian
0 Kudos
4 Replies
MarAlcaraz1
New Contributor
Hi Brain, try with a selection set and, then, a cursor. Mar
0 Kudos
RichardFairhurst
MVP Honored Contributor
Here is an example add-in button that does the basics of what you want:

Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.ADF

Imports My

Public Class MyClass ' Your real class name will appear here
  Inherits ESRI.ArcGIS.Desktop.AddIns.Button

  Public Sub New()

  End Sub

  Protected Overrides Sub OnClick()
    Dim pMxDoc As IMxDocument
    pMxDoc = CType(ArcMap.Application.Document, IMxDocument)
    Dim pMap As IMap
    pMap = pMxDoc.FocusMap
    Dim pEnumLayer As IEnumLayer
    pEnumLayer = pMap.Layers
    pEnumLayer.Reset()
    Dim pLayer As ILayer
    pLayer = pEnumLayer.Next
    Dim pFLayer As IFeatureLayer
    Do While Not pLayer Is Nothing
      If pLayer.Name = "MY_LAYER_NAME" And TypeOf pLayer Is IFeatureLayer Then ' Substitute your real layer name
        pFLayer = TryCast(pLayer, IFeatureLayer)
        Exit Do
      End If
      pLayer = pEnumLayer.Next
    Loop
    
    If pFLayer is Nothing Then
      MsgBox("No Layer Found")
      Exit Sub
    End If

    Dim pFSel As IFeatureSelection
    pFSel = pFLayer
    Dim pSelSet As ESRI.ArcGIS.Geodatabase.ISelectionSet2
    pSelSet = pFSel.SelectionSet
    If pSelSet.Count < 1 Then
      MsgBox("No Features Selected")
      Return
    End If

    Using comReleaser As ComReleaser = New ComReleaser() ' Recommended for cleaning up cursors
       Dim pFCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor = Nothing
       pSelSet.Search(Nothing, False, pFCursor)
       comReleaser.ManageLifetime(pFCursor)
       Dim pFeature As IFeature
       pFeature = pFCursor.NextFeature ' Get First Feature
       If pFeature Is Nothing Then
         MsgBox("No Features Selected!") ' Just in case, but probably already taken care of earlier
         Return
       End If

       Dim lFIndex As Long
       lFIndex = pFCursor.FindField("MY_FIELD_NAME") ' Replace with your field name
       If lFIndex > -1 Then
         Dim fValue as String ' Substute the correct data type to match the field type
         Do While Not pFeature Is Nothing
            fValue = pFeature.Value(lFIndex)
            ' Do something with the value stored in fValue
            pFeature = pFCursor.NextFeature
         Loop
      End If
    End Using
  End Sub

  Protected Overrides Sub OnUpdate()

  End Sub
End Class
BrianWestfall
New Contributor
Richard,

Thank you very much for the code. It works perfectly.

Brian
0 Kudos
BrianWestfall
New Contributor
Hi Brain, try with a selection set and, then, a cursor. Mar


Mar - thanks for the suggestion.

Brian
0 Kudos