1

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
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Aug 3, 2015 at 16:39
5
  • 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. Commented Aug 3, 2015 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!! Commented Aug 3, 2015 at 16:59
  • forgotten.... how can I just show alias name, please? Commented Aug 3, 2015 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 Commented Aug 3, 2015 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.... Commented Aug 3, 2015 at 17:40

1 Answer 1

3

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.

answered Aug 3, 2015 at 18:48
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.