vb.net arcobjects show fields from feature

3699
7
Jump to solution
08-03-2015 11:50 AM
SusanneWagner
New Contributor II

A big hello,

I try to show the field names (alias names) of a feature class. I found a 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)

Thanks a lot for any help!!

Imports ESRI.ArcGIS.esriSystem

Imports ESRI.ArcGIS.Carto

Imports ESRI.ArcGIS.ArcMapUI

Imports ESRI.ArcGIS.Geodatabase


Public Class AnzahlFelderButton

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)

      Dim fields As IFields = featureClass.Fields

      Dim field As IField = Nothing


      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)

           End If

      Next

      MsgBox(field.AliasName)

      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


 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

I'm glad to help. Please use the "Correct Answer" icon on the appropriate post to signify that this question has been answered (and also mark any posts that has been helpful). This will help others if they are searching for similar queries on this subject.

View solution in original post

7 Replies
KenBuja
MVP Esteemed Contributor

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

Although not mentioned in this post, but in the comment on this question on GIS.StackExchange, you mentioned that ArcMap was crashing with a application error. When this happens, use Try..Catch blocks in your code to figure out what's the problem.

Protected Overrides Sub OnClick()
  Try
    ShowDistinctFieldAliasNames(My.ArcMap.Document)
  Catch ex As Exception
    System.Windows.Forms.MessageBox.Show(ex.ToString, "Error", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error)
  End Try
End Sub
0 Kudos
SusanneWagner
New Contributor II

Dear Ken,

thank you a lot for you help and that you also answered me in the other discussion! You see I have big difficulties to understand this subject

You explained it great that I have to verify an active layer as feature class and I modified the code. For me its now a bit lighter in darkness but there is still a bigger mistake inside my code. Unluckily also the try and catch command doesn't show me anything. May I ask you again if you could have a look to the code what could be wrong?

Thank you a lot in advance!!

Imports ESRI.ArcGIS.esriSystem

Imports ESRI.ArcGIS.Carto

Imports ESRI.ArcGIS.ArcMapUI

Imports ESRI.ArcGIS.Geodatabase

Public Class AnzahlFelderButton

Inherits ESRI.ArcGIS.Desktop.AddIns.Button


Public Sub New()

End Sub


Protected Overrides Sub OnClick()

      Try

           ShowDistinctFieldAliasNames(My.ArcMap.Document)

      Catch ex As Exception

           System.Windows.Forms.MessageBox.Show(ex.ToString, Windows.Forms.MessageBoxButtons.OK,        Windows.Forms.MessageBoxIcon.Error)

      End Try

End Sub


Shared Sub ShowDistinctFieldAliasNames(ByVal featureClass As IFeatureClass)


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


 

' 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)

      End If

Next

MsgBox(field.AliasName)

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


 

0 Kudos
KenBuja
MVP Esteemed Contributor

I apologize for not being clear enough in my response. The code I showed for the Try..Catch was using your original code. Here's what you should have for the OnClick function.

Protected Overrides Sub OnClick()
  Dim pLayer As ESRI.ArcGIS.Carto.ILayer
  Dim pFLayer As ESRI.ArcGIS.Carto.IFeatureLayer
    Try
        pLayer = My.ArcMap.Document.FocusMap.Layer(0)
        If TypeOf pLayer Is ESRI.ArcGIS.Carto.IFeatureLayer Then
            pFLayer = pLayer
            ShowDistinctFieldAliasNames(pFLayer.FeatureClass)
        End If
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.ToString,"Error", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error)
    End Try
End Sub

This will return the first layer in the map and run the function on it. If you have many layers and need to do this on a specific one, then you'll have to add additional code to loop through all the layers.

0 Kudos
SusanneWagner
New Contributor II

Hello Ken,

thank you a lot for further help!! Now, it starts in ArcGis and also shows the first field of the table Unluckily after that I receive error message:

I don't know how to translate it correctly from german but it is said that the com-object of type "system._comObject" can't be changed into interface of "ESRI.Arcgis.Geodatabase.IFeatureClass".

error.JPG

May I ask you again if you recognise some other wrong parts in the code above?

Thank you in advance!!

0 Kudos
KenBuja
MVP Esteemed Contributor

This code works correctly, giving me the two fields with aliases in the dataset that I tested it on. One note, though. This will not show any aliases that you have made to the layer in the map document, only aliases that are in the dataset.

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

  Public Sub New()

  End Sub

  Protected Overrides Sub OnClick()

        Dim pLayer As ESRI.ArcGIS.Carto.ILayer
        Dim pFLayer As ESRI.ArcGIS.Carto.IFeatureLayer
        Try
            pLayer = My.ArcMap.Document.FocusMap.Layer(0)
            If TypeOf pLayer Is ESRI.ArcGIS.Carto.IFeatureLayer Then
                pFLayer = pLayer
                ShowDistinctFieldAliasNames(pFLayer.FeatureClass)
            End If
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Error", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error)
        End Try

    End Sub

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

    Shared Sub ShowDistinctFieldAliasNames(pFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass)
        Try

            Dim fields As ESRI.ArcGIS.Geodatabase.IFields = pFClass.Fields
            Dim field As ESRI.ArcGIS.Geodatabase.IField

            For i As Integer = 0 To fields.FieldCount - 1
                field = fields.Field(i)
                If field.Name <> field.AliasName Then
                    Debug.WriteLine(field.Name & ", " & field.AliasName)
                End If
            Next
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Sub Error", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error)
        End Try

    End Sub

End Class
SusanneWagner
New Contributor II

Dear Ken,

thank you again!!! You are great!!! The add-in works perfectly!  Thank you!!

0 Kudos
KenBuja
MVP Esteemed Contributor

I'm glad to help. Please use the "Correct Answer" icon on the appropriate post to signify that this question has been answered (and also mark any posts that has been helpful). This will help others if they are searching for similar queries on this subject.