Select to view content in your preferred language

Migrating VBA Script to .Net

917
6
07-07-2011 04:38 AM
KeaganAllan
Occasional Contributor
Ok,

This is my last attempt at this, I have been trying for the last day to move one script from 9.3.1 to 10.

I have a small Function that runs within a larger VBA script, so far from what I can tell, it falls down in this section when I have converted it to VB.Net:

Private Function GetLayer(sLayer) As ILayer

  Dim pMxDoc As IMxDocument
  Dim i As Integer
  Dim players As IEnumLayer
  Dim pLayer As ILayer
  Dim pL As ILayer
  Dim ii As Integer
  Dim pCL As ICompositeLayer
  Dim pApp As IApplication
  Set pApp = New AppRef
 
  On Error GoTo GetLayer_Err <---- It doesnt pass this point...

  If TypeOf pApp.Document Is IMxDocument Then
 
    Set pMxDoc = pApp.Document
    Set players = pMxDoc.FocusMap.layers(, True)
   
  End If
....

It does not pass -

pApp = New AppRef

It will also not allow me to use the IApplication due to a "friend" error.
Please could someone help me understand why this is the case?

I am still very new to scripting and have been relying on online samples and help to get me here. From what I can tell this is the last hurdle.

I am using Visual Studio Express 2008
Vb.Net and creating an ArcMap 10 Add-In.

Any and all comments / help will be greatly appreciated.

Thank you!!
0 Kudos
6 Replies
KeaganAllan
Occasional Contributor
Ok...

I dont know how this works...
If someone could please explain it to me I would REALLY appreciate where I went wrong and WHY this works.

I ended up adding the following at the start of the module:

Private pApp As AppRef

Then where my Function is kept I added the following:

Dim pApp as Application
pApp = My.ArcMap.Application

I can't believe this held me back....
0 Kudos
RuchiraWelikala
Regular Contributor
I don't think you're grabbing the MxDocument correctly. 
Can you show us the VB.NET code you have written (if any)?  Right now it seems we're only looking at the VBA code.

Thanks,
0 Kudos
KeaganAllan
Occasional Contributor
Hi,

Sorry when I posted that I was very frustrated.
The full (now working .Net Code is below).
Please note,I have only posted the Function code as the whole script is VERY long.

    Public Function GetLayer(ByVal sLayer) As ILayer

        Dim pMxDoc As IMxDocument
        Dim i As Integer
        Dim players As IEnumLayer
        Dim pLayer As ILayer
        Dim pL As ILayer
        Dim ii As Integer
        Dim pCL As ICompositeLayer
        Dim pApp As Application
        pApp = My.ArcMap.Application

        On Error GoTo GetLayer_Err

        If TypeOf pApp.Document Is IMxDocument Then

            pMxDoc = pApp.Document
            players = pMxDoc.FocusMap.Layers(, True)

        End If

        pLayer = players.Next
        Do While Not pLayer Is Nothing
            If TypeOf pLayer Is ICompositeLayer Then
                pCL = pLayer
                For ii = 0 To pCL.Count - 1
                    pL = pCL.Layer(ii)
                    If UCase(pL.Name) = UCase(sLayer) Then
                        GetLayer = pL
                        Exit Function
                    End If
                Next
            Else
                If UCase(pLayer.Name) = UCase(sLayer) Then
                    GetLayer = pLayer
                    Exit Function
                End If
            End If
            pLayer = players.Next
        Loop

        Exit Function

GetLayer_Err:

    End Function
0 Kudos
RuchiraWelikala
Regular Contributor
When you create a command through VB.NET, it sets up the hook for the application automatically.
Looks like this at the beginning of your code:
NOTE:  This is autogenerated code! Should already exist in your base command!
   
Public Overrides Sub OnCreate(ByVal hook As Object)
        If Not hook Is Nothing Then
            m_application = CType(hook, IApplication)

            'Disable if it is not ArcMap
            If TypeOf hook Is IMxApplication Then
                MyBase.m_enabled = True
            Else
                MyBase.m_enabled = False
            End If
        End If
        ' TODO:  Add other initialization code
    End Sub


So your code should look something like this:
Public Function GetLayer(ByVal sLayer) As ILayer

Dim pMxDoc As IMxDocument
Dim i As Integer
Dim players As IEnumLayer
Dim pLayer As ILayer
Dim pL As ILayer
Dim ii As Integer
Dim pCL As ICompositeLayer

pMxDoc = m_application.Document
On Error GoTo GetLayer_Err

players = pMxDoc.FocusMap.Layers(, True)

End If

pLayer = players.Next
Do While Not pLayer Is Nothing
If TypeOf pLayer Is ICompositeLayer Then
pCL = pLayer
For ii = 0 To pCL.Count - 1
pL = pCL.Layer(ii)
If UCase(pL.Name) = UCase(sLayer) Then
GetLayer = pL
Exit Function
End If
Next
Else
If UCase(pLayer.Name) = UCase(sLayer) Then
GetLayer = pLayer
Exit Function
End If
End If
pLayer = players.Next
Loop

Exit Function

GetLayer_Err:

End Function 


Should work.
0 Kudos
KeaganAllan
Occasional Contributor
Ok,

So in my main module (where Public Overides XXXX On_Click) is, I should have added the "Hook" phrases?

In the end what I put forward has worked, I am not sure why though.

Thanks for your responses, I am still learning and your patience is greatly appreciated.
0 Kudos
RuchiraWelikala
Regular Contributor
The OnCreate Sub is right above the OnClick sub (or should be). They are autogenerated in VB.NET when you create the application. 
I've never used the "My.MyMap..." method before for getting the Document or the application from ArcMap.  That's one of the key differences between VBA and .NET.  VBA used ThisDocument while .NET uses m_application.
0 Kudos