Convert vba code  to C# for arc 10

516
3
03-15-2011 12:21 PM
jamescurtis
New Contributor
I have a code snippet of VBA and I need to convert this to C# code Very Quickly. Can anyone help

Private Sub SignPost_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)

   ' Create connection string to connect to SQL Server
    Dim ConnectStr As String
    ConnectStr = "SERVER=esriappprod.memphis.edu;INSTANCE=5151;VERSION=cpgismob1;USER=cofm;PASSWORD=uofm"

    ' Open the SQL Server Workspace Factory
    Dim pWSF2 As IWorkspaceFactory2
    Set pWSF2 = New SdeWorkspaceFactory
    Dim pWS As IWorkspace
    Set pWS = pWSF2.OpenFromString(ConnectStr, 0)
    
    ' Start Editing
    Set pWSEdit = pWS
    pWSEdit.StartEditing True
    pWSEdit.StartEditOperation

    ' Open the feature class
    Dim pFeatureWS As IFeatureWorkspace
    Set pFeatureWS = pWS
    Dim pFClass As IFeatureClass
    Set pFClass = pFeatureWS.OpenFeatureClass("COFM.SignPost")
    
    ' Create new Feature Layer and Point.
    Dim pFL As IFeatureLayer2
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Dim pBasicMap As IBasicMap
    Set pBasicMap = pMap
    Dim pMxApplication As IMxApplication
    Set pMxApplication = Application
    Dim pRow As IRow
    Dim pRowObj As IRowEdit
    Set pRow = pRowObj
   
    Dim pMVal As Long
    Set pFeat = pFClass.CreateFeature
    Dim pPoint As IPoint
    Set pPoint = pMxDoc.CurrentLocation
    Set pFeat.Shape = pPoint
    Dim pEnvelope As IEnvelope
    Set pEnvelope = pPoint.Envelope
    
    Dim pIdObj As IIdentifyObj
    Dim pFeatIdObj As IFeatureIdentifyObj
    
    Set pFeatIdObj = New FeatureIdentifyObject
    pFeatIdObj.Feature = pFeat
    
    Set pIdObj = pFeatIdObj
    pIdObj.Flash pMxApplication.Display
    
    pFeat.Store
    pMap.SelectByShape pEnvelope, pMxApplication.SelectionEnvironment, True
       
    'Populates the "MOBILEUPDATE" field with the date and time the point was created
    Dim MyDate As Date
    MyDate = FormatDateTime(Now, vbGeneralDate)
    pFeat.Value(pFeat.Fields.FindField("MOBILEUPDATE")) = MyDate
    pFeat.Store
       
    'Records VersionName and EditorName
    Dim pVer As String
    pVer = ("cpgismob1")
    pFeat.Value(pFeat.Fields.FindField("MOBILEVERSION")) = pVer
    Dim strNew As String
    strNew = LCase(strUserName)
    pFeat.Value(pFeat.Fields.FindField("MOBILEEDITOR")) = strNew
    
    ' Stop Editing and refresh active view
    Dim pAV As IActiveView
    Set pAV = pMap
    pWSEdit.StopEditing True
    pWSEdit.StopEditOperation
    pAV.Refresh
    
End Sub
0 Kudos
3 Replies
MichaelRobb
Occasional Contributor III
I would think going from VBA to VB.net would be easier.
Also, (Set pMxDoc = ThisDocument) is NOT going to work in either vb.net or C#

You will have to do a hook on the application or the IApplication.

m_application as IApplication
m_application = DirectCast(hook, IApplication)
if your running outside ArcMap, you have to go through the Approt and see which session you are using.

Then you can

pMxdoc = m_application.Document (Rather than THISDocument)


Or if you are running on a form created, I do it this way...
In the Command (Button)
In the ONCLick Event ICommand.CLick
'issue with the below, is the form duplicates

             Dim f1 As New frmUpdatePoints
                'pass the m_application along...

                f1.ArcGISApplication = m_application
                f1.Show() ' dont need anything back but keeps from the user playing with Arc while form is open



In the FORM
    Private m_application As ESRI.ArcGIS.Framework.IApplication

Public WriteOnly Property ArcGISApplication() As ESRI.ArcGIS.Framework.IApplication

        Set(ByVal value As ESRI.ArcGIS.Framework.IApplication)
            m_application = value
        End Set
    End Property


now you can use m_application as before

Have you tried any of the converters available online?
0 Kudos
jamescurtis
New Contributor
Thanks for your replay. But, the part you commented on i have done already. I basically need to know how to open featurelayer and add a point to it.
0 Kudos
MichaelRobb
Occasional Contributor III
Thanks for your replay. But, the part you commented on i have done already. I basically need to know how to open featurelayer and add a point to it.


But the part you have done was not mentioned in your initial post, nor was the fact that you were specifically looking for open featurelayer and adding a point.  Just saying, makes it somewhat difficult.
Also, correcting 'ThisDocument' issue you would have had, I would have thought would have been more of an issue than adding a featurelayer... (CartoClass)

Use the API's provided by ESRI or C# Developer Kit or perhaps someone with extensive C# can help you.

And will look something like this:

{
            IFeatureLayer featureLayer = new FeatureLayerClass();
            IFeatureClass featureClass = featureLayer.FeatureClass;

}
0 Kudos