0

I try to show the field names (alias names) of a feature class. I found code snippet but I don't succeed to get it to run :( Can somebody tell me please what is wrong in the code? (System: Arcgis 10.2, Visual Studio2012)

Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Geodatabase

Public Class FieldNameButton
    Inherits ESRI.ArcGIS.Desktop.AddIns.Button

    Public Sub New()
    End Sub

    Protected Overrides Sub OnClick()
        ShowDistinctFieldAliasNames(My.ArcMap.Document)
    End Sub


    Shared Sub ShowDistinctFieldAliasNames(ByVal featureClass As IFeatureClass)

        ' Get the Fields collection from the feature class.
        Dim fields As IFields = featureClass.Fields
        Dim field As IField = Nothing

        ' On a zero-based index, iterate through the fields in the collection.
        For i As Integer = 0 To fields.FieldCount - 1
            ' Get the field at the given index.
            field = fields.Field(i)
            If field.Name <> field.AliasName Then
                Console.WriteLine("{0} : {1}", field.Name, field.AliasName)
                MsgBox(field.AliasName)
            End If
        Next i
        My.ArcMap.Application.CurrentTool = Nothing
    End Sub

    Protected Overrides Sub OnUpdate()
        Enabled = My.ArcMap.Application IsNot Nothing
    End Sub

    Private Sub DisplayDistinctFieldAliasNames()
        Throw New NotImplementedException
    End Sub

End Class
  • Welcome to GIS SE. Can you tell us where it is hanging up when debugging? What kind of error is it giving and where? Also, the code you provide will only print the field name and field alias when they are not the same. – Barbarossa Aug 3 '15 at 16:45
  • Hello Barbarossa, thanks a lot for your quick answer!! I don't have any error message when I start visual studio. Just after trying in Arcgis , loading a feature and start the code with an Add-in button, ArcGis close itself just with telling me that there was heavy appication error. So, I don't have a specific error message about my code. But as I'm fully beginner I assume that I have big mistake in code! Thanks for any further help!! – Susan Aug 3 '15 at 16:59
  • forgotten.... how can I just show alias name, please? – Susan Aug 3 '15 at 17:02
  • In Visual Studio, you can debug your addin-in (small play button at the top of the screen) if ArcMap is the default application See here – Barbarossa Aug 3 '15 at 17:03
  • Dear Barbarossa, thank you again. I already start debugging in this way. I finally just see the result: in my case ArcGis shuts down itself and just with the heavy application error - no further explanations.... – Susan Aug 3 '15 at 17:40
2

Your function is expecting a feature class

Shared Sub ShowDistinctFieldAliasNames(ByVal featureClass As IFeatureClass)

but you're passing in a document

ShowDistinctFieldAliasNames(My.ArcMap.Document)

To pass in a feature class, you'll have to find a layer in that document and verify that it's a feature class. Something like this:

dim pLayer As ESRI.ArcGIS.Carto.ILayer
dim pFLayer As ESRI.ArcGIS.Carto.IFeatureLayer

pLayer= My.ArcMap.Document.FocusMap.Layer(0)
if TypeOf pLayer Is ESRI.ArcGIS.Carto.IFeatureLayer then
    pFLayer = pLayer
    ShowDistinctFieldAliasNames(pFLayer.FeatureClass)
end if

If you ever have a case where ArcMap crashes with an application error, use Try..Catch blocks in your code to figure out what's the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.