Code Migration from VBA to VB.NET

474
4
04-08-2011 12:44 AM
DeniseL
New Contributor
I want to migrate some little codes from VBA to VB.NET, but I don't understand any online help, because I'm no programmer! It's only some little code, like to add a field in a table. I have got the VBA code here:

-----------------------------------------------------------------------------------
Sub AddField_Name1()
On Error GoTo ErrorHandler
'
'
Dim pMxDoc As IMxDocument
Dim pLayer As ILayer
Dim pFLayer As IFeatureLayer
Dim pFeatClass As IFeatureClass
Dim NewField As IFieldEdit
Dim strFieldName As String
Dim intFieldType As Integer
Dim intLength As Integer
'
'
'Verweis auf das aktuelle Dokument
Set pMxDoc = ThisDocument
'
Set pLayer = pMxDoc.SelectedLayer
If pLayer Is Nothing Then
MsgBox "Achtung, es ist kein Layer selektiert"
Exit Sub
End If
Set pFLayer = pLayer
Set pFeatClass = pFLayer.FeatureClass
'
' Eingabe der Feldnamen
strFieldName = "Name1"
'
'
intFieldType = 4
intLength = 75
Set NewField = New Field
With NewField
.Length = intLength
.Type = intFieldType
.Name = strFieldName
'
' Kontrolle, ob Feldname schon vorhanden
If pFeatClass.FindField(strFieldName) > -1 Then
  MsgBox "Das Feld '" & strFieldName & "' ist bereits vorhanden, bitte anderen Feldnamen angeben!"
  Exit Sub
  Else
  'MsgBox "Das Feld '" & strFieldName & "' ist noch nicht vorhanden und wird erzeugt!"
End If
'
pFeatClass.AddField NewField 'Neues Feld wird hinzugefügt
'
Exit Sub
'
ErrorHandler:
MsgBox "Es ist ein Fehler aufgetreten, das Programm wird beendet!"
'
'
End With
End Sub
-------------------------------------------------------------------------------------
Can somebody give me some help?? I Don't know what to do. I followed the beginning steps in VisualStudio 2008, but where do I have to put snippets and References?

I Hope somebody of you as a little time to help me :)! Please

Thank you for your help!

Greetings
0 Kudos
4 Replies
MichaelRobb
Occasional Contributor III
Well, its not that easy...
Pending which version as well, 9.3 prior, you must make the toolbar/button with IToolbarDef code... then its hooking to the application from there...  registering the dll using regasm32 on a windows xp machine
If you are using 10, you then use addins, now you have to write the XML and such...

You will notice .THISdocument is now replaced wtih namespace MY when using addins...


Here is your converted code, but unfortunately, its not just going to be a copy paste, publish and it works.  There is more involved.  e.g. Gathering the references associating the class into ArcMap etc.... button etc.
Not sure, but your code is just simply adding a field, perhaps a model would suit you better.


Imports System.Windows.Forms
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geodatabase





Public Class Class1
    Sub AddField_Name1()

        Try


            '
            '
            Dim pMxDoc As IMxDocument
            Dim pLayer As ILayer
            Dim pFLayer As IFeatureLayer
            Dim pFeatClass As IFeatureClass
            Dim NewField As IFieldEdit
            Dim strFieldName As String
            Dim intFieldType As Integer
            Dim intLength As Integer
            '
            '
            'Verweis auf das aktuelle Dokument
            pMxDoc = My.ArcMap.Document 'ThisDocument

            '
            pLayer = pMxDoc.SelectedLayer
            If pLayer Is Nothing Then
                MessageBox.Show("Achtung, es ist kein Layer selektiert", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End If
            pFLayer = pLayer
            pFeatClass = pFLayer.FeatureClass
            '
            ' Eingabe der Feldnamen
            strFieldName = "Name1"
            '
            '


            NewField = New Field
            With NewField
                .Length_2 = 75
                .Type_2 = esriFieldType.esriFieldTypeString
                .Name_2 = strFieldName
            End With
            '
            ' Kontrolle, ob Feldname schon vorhanden
            If pFeatClass.FindField(strFieldName) > -1 Then
                MessageBox.Show("Das Feld '" & strFieldName & "' ist bereits vorhanden, bitte anderen Feldnamen angeben!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            Else
               MessageBox.Show( "Das Feld '" & strFieldName & "' ist noch nicht vorhanden und wird erzeugt!")
            End If
            '
            pFeatClass.AddField(NewField) 'Neues Feld wird hinzugefügt
            '


        Catch ex As Exception
            MessageBox.Show("Es ist ein Fehler aufgetreten, das Programm wird beendet!", "", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub
End Class
0 Kudos
DeniseL
New Contributor
Thank you very very much for your answer!!
I'm using ArcGIS 10 now, the original code is from 9.3.1!

Good idea to put it into the model builder, I will try this soon!
But I hava another question, where do I have to put this code?
I used the Add In Wizard and created a Button, I Think I have to put your code under the lines

   Protected Overrides Sub OnClick()

Is this right?

Thank you and have a nice weekend!
Greetings
0 Kudos
RuchiraWelikala
Occasional Contributor
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Migrating_ArcGIS_9_3_De...

Make sure you visit that site.  Some of the assemblies from 9.3 have been deprecated/changed in 10, so you will have to look out for those. 

Cheers,
0 Kudos
OlaRennemo
New Contributor III
My guess is that you end up rewriting everything, in order to take advantage of better project management and structures...
0 Kudos