0

have anybody codesnippets or complete code in vb.net to read and show the alias fieldnames of an attribute table of an shapefile in arcgis?

To open the Shapefile, I have the following code:

Public Function PersonalGeodatabaseÖffnen() As IWorkspace
        Const path As String = "C:\\DatabaseGIS\xxx.mdb"
        Dim workspaceFactory As IWorkspaceFactory2 = New       SapefileWorkspaceFactoryClass
        Dim workspace As IWorkspace = workspaceFactory.OpenFromFile(path, 0)
        Return workspace
        MsgBox(path)
    End Function
  • 1
    You've got as far as the workspace but... your function is called PersonalGeodatabase and the path is an mdb so Shapefile Workspace is not correct, it should be Personal (Access) Database connection.. So is it shapefile or personal geodatabase you are after? What do you want to do with the aliases? i.e. how do you want them returned? On a side note the MsgBox will never be hit as it's after the return and your path is half 'c' style and half 'VB' style.. I would suggest for VB it should be "C:\DatabaseGIS\xxx.mdb" – Michael Stimson Aug 28 '16 at 21:59
1

Expanding from my comment here's a bit of code that might help:

     Private Function GetAliasList(pPath As String, pShapeName As String) As List(Of String)
        Dim AliasReturnList As List(Of String) = New List(Of String)
        ' Open the workspace'
        Dim pWorkspaceFactory As IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory()
        Dim pWorkspace As IWorkspace = pWorkspaceFactory.OpenFromFile(pPath, 0)
        Dim pFeatureWorkspace As IFeatureWorkspace = CType(pWorkspace, IFeatureWorkspace)   ' need IFeatureWorkspace to open a feature class'

        ' Open the shapefile in the workspace'
        Dim pFeatClass As IFeatureClass = pFeatureWorkspace.OpenFeatureClass(pShapeName)

        For pCnt As Integer = 0 To pFeatClass.Fields.FieldCount - 1
            ' access each field in turn.. don't really need to cast to a '
            ' field object but I'll do it for clarity, normally I'd access''
            ' directly using pFeatClass.Fields.Field(pCnt)'
            Dim pThisField As IField = pFeatClass.Fields.Field(pCnt)
            AliasReturnList.Add(pThisField.AliasName)   ' add the AliasName to the list
            ' note that if an alias is not set it defaults to the field name'
        Next
        ' now return it..'
        Return AliasReturnList
    End Function

You've got as far as connecting to the workspace but you need an IFeatureWorkspace to open a feature class, from the IFeatureClass you can iterate the IField objects from IFeatureClass.Fields to obtain the AliasName of each field. Note that this is the same for file GDB, personal GDB, shapefiles, CAD etc.. except the IWorkspaceFactory needs to be created from the correct CoClass (see the CoClasses here, there's a lot of them), for shapefiles it's ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory and for personal geodatabase it's ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory.

In your code sample the messagebox will never be hit because it's after the Return statement.. nothing (besides end function) should appear after a return statement because the process returns immediately and goes no further.

Your path as specified is half 'C' and half 'VB', C uses the slash as an escape character which is why it's always doubled (\\ => \), VB uses different escaping so the slashes are not doubled.

Don't feel I'm trying to be mean when I point out mistakes in your code, I wouldn't really be helpful if I showed you how to get an alias name but didn't point out the obvious mistakes.

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.