Select to view content in your preferred language

Converting VBA code into C#

495
2
11-27-2010 02:45 AM
azharhussain
New Contributor
I am unable to convert the code into C#, Could you please help me in this regards.


Sub Joindata()

Dim mx As IMxDocument
Dim pMap As IMap
Set mx = ThisDocument
Set pMap = mx.FocusMap
' The 1st layer in the table of contents must
' be a point layer and the second must be an
' area layer.

Dim pPntLayer As IFeatureLayer
Dim pAreaLayer As IFeatureLayer
Set pPntLayer = pMap.Layer(0)
Set pAreaLayer = pMap.Layer(1)

' Get the output workspace name - make it
' the same as the input polygon layer in the join
Dim pDataset As IDataset
Dim pWkSpDataset As IDataset
Dim pWkSpName As IWorkspaceName
Set pDataset = pAreaLayer.FeatureClass
Set pWkSpDataset = pDataset.Workspace
Set pWkSpName = pWkSpDataset.FullName

' create the name object for the output join by location shapefile
Dim strOutName As String
Dim pFCName As IFeatureClassName
Dim pOutDSName As IDatasetName
Dim pName As IName
strOutName = InputBox("Enter the output shapefile name, it will create in input folder:")

Set pFCName = New FeatureClassName
With pFCName
.FeatureType = esriFTSimple
.ShapeFieldName = "Shape"
.ShapeType = esriGeometryPolygon
End With
Set pOutDSName = pFCName
With pOutDSName
.name = strOutName
Set .WorkspaceName = pWkSpName
End With
Set pName = pOutDSName

' Do a join by location that joins the attributes of the
' first point cantained within each polygon.
Dim pSpJoin As ISpatialJoin
Dim pFCNew As IFeatureClass

Dim paggopts As IAggregateOptions

Set pSpJoin = New SpatialJoin
With pSpJoin
Set .JoinTable = pPntLayer.FeatureClass
Set .SourceTable = pAreaLayer.FeatureClass


.LeftOuterJoin = True
End With

Set paggopts = pSpJoin
paggopts.IsSum = True


' setting maxMapDist to 0 means that only points within
' each each polygon will be considered
Set pFCNew = pSpJoin.JoinAggregate(pName, 0)

MsgBox "Finished"

End Sub
0 Kudos
2 Replies
Venkata_RaoTammineni
Regular Contributor
Hi,

Please find the below code converted into C#

  public override void OnClick()
        {

            try
            {
                IMxDocument pMxDoc = null;
                ESRI.ArcGIS.Carto.IMap pMap = null;
                ESRI.ArcGIS.Carto.IFeatureLayer pFLayer1 = null;
                IFeatureLayer pFLayer2 = null;


                pMxDoc = m_application.Document as IMxDocument;
                pMap = pMxDoc.FocusMap;
                pFLayer1 = pMap.get_Layer(0) as IFeatureLayer;
                pFLayer2 = pMap.get_Layer(1) as IFeatureLayer;

                IDataset pDataset = null;
                IDataset pwkDataset = null;
                IWorkspaceName Pwkspname = null;

                pDataset = pFLayer2.FeatureClass as IDataset;

                pwkDataset = pDataset.Workspace as IDataset;
                Pwkspname = pwkDataset.FullName as IWorkspaceName;


                String stroutname;
                IFeatureClassName pFCName = null;
                IDatasetName pOutDSName = null;
                ESRI.ArcGIS.esriSystem.IName pName = null;

                stroutname = "Outputfile name"; //C# doenst have Inputbox...May be some 3rd party dll u need to use to get inputbox in C#

                pFCName = new FeatureClassNameClass();

                pFCName.FeatureType = esriFeatureType.esriFTSimple;
                pFCName.ShapeFieldName = "Shape";
                pFCName.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;

                pOutDSName = pFCName as IDatasetName;
                pOutDSName.Name = stroutname;
                pOutDSName.WorkspaceName = Pwkspname;


                pName = pOutDSName as IName;

                ISpatialJoin pspjoin;
                IFeatureClass pFCnew = null;
                IAggregateOptions paggoptions = null;

                pspjoin = new SpatialJoin();

                pspjoin.JoinTable = pFLayer1.FeatureClass as ITable;
                pspjoin.SourceTable = pFLayer2.FeatureClass as ITable;
                pspjoin.LeftOuterJoin = true;


                paggoptions = pspjoin as IAggregateOptions;

                paggoptions.IsSum = true;

                pFCnew = pspjoin.JoinAggregate(pName, 0);
            }
            catch (Exception exp) { }
        }

Thanks and Regards,

Venkat Tammineni
0 Kudos
Venkata_RaoTammineni
Regular Contributor
is that working fine ?
0 Kudos