Help with syntax in Visual Basics for ArcGIS Pro SDK

887
6
Jump to solution
12-12-2023 07:04 AM
DuncanHornby
MVP Notable Contributor

Hi

I'm hoping someone from esri can shed some light  on some simple coding style.  As requested in this idea there is a remarkable lack of sample code in Visual Basic.  I've spent much of my career using VB and ArcObjects in ArcMap and now facing the challenge of developing in ArcPro.  I would like to avoid having to sink months/possibly years into becoming  proficient in c# when I know VB!

I understand that beneath the hood ArcPro is a very different beast requiring  different coding styles because of its multi-threaded nature and that many things have to be done differently.  That's OK when simple, easy to digest samples are provided to fast track ones self.

I've discovered that many of the objects I need to use must be run in a queuedtask but all the examples on the esri github site are all in the cryptic c# language.

I have some simple code that opens a layerfile on a onclick of a button.  I cannot for the life of me work out the syntax that must be used in the line QueuedTask.Run().

Friend Class Button1
    Inherits Button

    Protected Overrides Sub OnClick()
        ' How To complete the Next line?
        Await QueuedTask.Run()
    End Sub

    Private Function myCode() As LayerDocument
        Dim sp As String
        sp = "c:\scratch\mydata.lyrx"
        Dim ld As LayerDocument
        ld = New LayerDocument(sp)
        Return ld
    End Function
End Class

 

Can someone show how this code should be written and best practise?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

Hi,

Thanks for the pointer to the website. I had actually seen this thread before I posted (i.e. had done my research) and followed the link to the code conversion website and tried their example; problem was is when you paste it into visual studio 2022 it throws an error but gives no real indication why.  In my random stumbling around in the vastness of the internet I had seen some comment, probably on this forum, about what seems to be an undocumented addition to the code that needs to be done if you are developing a button for the ArcPro ribbon and that's the inclusion of the command "Async" in the  OnClick Sub declaration.  I've posted the solution over on GIS StackExchange but provide it here too. It provides a simple VISUAL BASIC template for building code that requires running as a QueuedTask.

 

 

 

    Protected Overrides Async Sub OnClick()
        ' Code assumes layerfile is referencing a geodatabase featureclass.

        Dim b As Boolean
        b = Await QueuedTask.Run(Function()
                                     ' Connect to layer file
                                     Dim sLayerFilePath As String
                                     sLayerFilePath = "c:\scratch\ORN2.lyrx"
                                     Dim lyrDoc As LayerDocument
                                     lyrDoc = New LayerDocument(sLayerFilePath)

                                     ' Create a CIM layer document object
                                     Dim cimLayerDoc As CIMLayerDocument
                                     cimLayerDoc = lyrDoc.GetCIMLayerDocument

                                     ' Create a CIM Feature Layer object
                                     Dim cimFeaturelyr As CIMFeatureLayer
                                     cimFeaturelyr = CType(cimLayerDoc.LayerDefinitions(0), CIMFeatureLayer)

                                     ' Create a CIM Standard Data Connection
                                     Dim cimSDCon As CIMStandardDataConnection
                                     cimSDCon = CType(cimFeaturelyr.FeatureTable.DataConnection, CIMStandardDataConnection)

                                     ' Create a full path name string to feature class
                                     Dim sPath As String
                                     Dim sFC As String
                                     sPath = cimSDCon.WorkspaceConnectionString
                                     sFC = cimSDCon.Dataset
                                     Dim sFullpath As String
                                     sFullpath = sPath & "\" & sFC
                                     sFullpath = sFullpath.Remove(0, 9) ' Removes DATABASE= from start of string

                                     MsgBox(sFullpath, MsgBoxStyle.Exclamation, "Feature class Path")
                                     Return True
                                 End Function)

    End Sub

 

 

 

 

If there is anyone from the ESRI documentation team reading this, it would be good if you expand your snippets on the GITHUB site to include some sample code in Visual Basic.  Why has ESRI abandoned VB? When you go to the esri git hub site and search for code language in VB it returns nothing!

View solution in original post

Tags (2)
0 Kudos
6 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Look at the thread.

Using converter you can convert c# code from sample to VB code. QueuedTask.Run could look like that:

Private Sub SurroundingSub()
    Dim gdbPath As String = "@C:\myDataFolder\myData.gdb"
    Dim newlyAddedGDB = Await QueuedTask.Run(Function()
                                                 Dim item = TryCast(ItemFactory.Instance.Create(gdbPath), IProjectItem)
                                                 Return If(Project.Current.AddItem(item), TryCast(item, GDBProjectItem), Nothing)
                                             End Function)
End Sub

 

DuncanHornby
MVP Notable Contributor

Hi,

Thanks for the pointer to the website. I had actually seen this thread before I posted (i.e. had done my research) and followed the link to the code conversion website and tried their example; problem was is when you paste it into visual studio 2022 it throws an error but gives no real indication why.  In my random stumbling around in the vastness of the internet I had seen some comment, probably on this forum, about what seems to be an undocumented addition to the code that needs to be done if you are developing a button for the ArcPro ribbon and that's the inclusion of the command "Async" in the  OnClick Sub declaration.  I've posted the solution over on GIS StackExchange but provide it here too. It provides a simple VISUAL BASIC template for building code that requires running as a QueuedTask.

 

 

 

    Protected Overrides Async Sub OnClick()
        ' Code assumes layerfile is referencing a geodatabase featureclass.

        Dim b As Boolean
        b = Await QueuedTask.Run(Function()
                                     ' Connect to layer file
                                     Dim sLayerFilePath As String
                                     sLayerFilePath = "c:\scratch\ORN2.lyrx"
                                     Dim lyrDoc As LayerDocument
                                     lyrDoc = New LayerDocument(sLayerFilePath)

                                     ' Create a CIM layer document object
                                     Dim cimLayerDoc As CIMLayerDocument
                                     cimLayerDoc = lyrDoc.GetCIMLayerDocument

                                     ' Create a CIM Feature Layer object
                                     Dim cimFeaturelyr As CIMFeatureLayer
                                     cimFeaturelyr = CType(cimLayerDoc.LayerDefinitions(0), CIMFeatureLayer)

                                     ' Create a CIM Standard Data Connection
                                     Dim cimSDCon As CIMStandardDataConnection
                                     cimSDCon = CType(cimFeaturelyr.FeatureTable.DataConnection, CIMStandardDataConnection)

                                     ' Create a full path name string to feature class
                                     Dim sPath As String
                                     Dim sFC As String
                                     sPath = cimSDCon.WorkspaceConnectionString
                                     sFC = cimSDCon.Dataset
                                     Dim sFullpath As String
                                     sFullpath = sPath & "\" & sFC
                                     sFullpath = sFullpath.Remove(0, 9) ' Removes DATABASE= from start of string

                                     MsgBox(sFullpath, MsgBoxStyle.Exclamation, "Feature class Path")
                                     Return True
                                 End Function)

    End Sub

 

 

 

 

If there is anyone from the ESRI documentation team reading this, it would be good if you expand your snippets on the GITHUB site to include some sample code in Visual Basic.  Why has ESRI abandoned VB? When you go to the esri git hub site and search for code language in VB it returns nothing!

Tags (2)
0 Kudos
GKmieliauskas
Esri Regular Contributor

Sample for VB here

DuncanHornby
MVP Notable Contributor

Good find! That is hard to find on the  esri github site. What blows my mind is that it appears to be the only sample in VB...

0 Kudos
GKmieliauskas
Esri Regular Contributor

I download all samples zip from github, extract it locally and do search for method or object name or file extension

StephenRhea_NV5
Occasional Contributor

Glad you found the answer! I've used a code converter like https://converter.telerik.com/ with a lot of success. It's admittedly tedious with larger code sets, but it's a great jumping off point.

0 Kudos