1. VBA: variable = object.FieldCount
C#: public int FieldCount {get; }
Example
VBA: Set pFields = New Fields
Set pFieldsEdit = pFields
pFieldsEdit.FieldCount = 3
C#: pFields = new FieldsClass();
pFieldsEdit = (IFieldsEdit)pFields;
pFieldsEdit.FieldCount_2 = 3;
2. Set two a attribute fields by cloning from existing table (IClone)
VBA: Dim pClone As IClone
Set pClone = pTable.Fields.Field(l_A1)
Set pFieldsEdit.Field(1) = pClone.Clone
Set pClone = pTable.Fields.Field(l_A2)
Set pFieldsEdit.Field(2) = pClone.Clone
C#: IClone pClone;
pClone = (IClone)pTable.Fields.get_Field(l_A1);
pFieldsEdit.set_Field(1,(IField)pClone.Clone());
pClone = (IClone)pTable.Fields.get_Field(l_A2);
pFieldsEdit.set_Field(2, (IField)pClone.Clone());
Here the setting of field has taken 2 parameters
3. ISegmentCollection
VBA: Set pLine = CreateLn(CreatePt(pRow.Value(l_X1), pRow.Value(l_Y1)), CreatePt(pRow.Value(l_X2), pRow.Value(l_Y2)))
Set pLine = CreateLn(pFromPoint, pToPoint)
Set pSegColl = New Path
pSegColl.AddSegment pLine
Set pGeomColl = New Polyline
pGeomColl.AddGeometry pSegColl
Set pFeat = pFeatClass.CreateFeature
Set pPolyline = pGeomColl
C#: pLine = CreateLn(pFromPoint, pToPoint);
pSegColl = new PathClass();
object Missing = Type.Missing;
pSegColl.AddSegment((ISegment)pLine, ref Missing , ref Missing);
pGeomColl = new PolylineClass();
pGeomColl.AddGeometry((IGeometry)pSegColl, ref Missing, ref Missing);
pFeat = pFeatClass.CreateFeature();
pPolyline = (IPolyline)pGeomColl;
Here both AddSegment and AdGeometry take 3 parameters and the object has been set as Type.Missing
4. Set the Feature's Shape and the specified attributes
VBA: Set pFeat.Shape = pPolyline
pFeat.Value(l_FCA1) = pRow.Value(l_A1)
pFeat.Value(l_FCA2) = pRow.Value(l_A2)
C#: pFeat.Shape = pPolyline;
pFeat.set_Value(l_FCA1, pRow.get_Value(l_A1));
pFeat.set_Value(l_FCA2, pRow.get_Value(l_A2));
In VBA row vales are read in feature value by equating whereas in C# the values are set with index and the object parameter.
C# to create polyline using the X,Y values from DBF Table
//Create polyline shape file directly from the table between two X and Y
private void CreatePolyLines()
{
string m_sX1 = "X";
string m_sY1 = "Y";
string m_sX2 = "X";
string m_sY2 = "Y";
string m_sAttrib1 = "XYZ";
string m_sAttrib2 = "ID";
IWorkspaceFactory pFact;
IWorkspace pWorkspace;
IFeatureWorkspace pFeatws;
ITable pTable;
pFact = new ShapefileWorkspaceFactoryClass();
pWorkspace = pFact.OpenFromFile(@"pointtoapath", 0);
pFeatws = (IFeatureWorkspace)pWorkspace;
pTable = pFeatws.OpenTable("TableName");
// Find Fields containing X and Y coordinates, and the specified attributes.
int l_X1, l_Y1, l_X2, l_Y2, l_A1, l_A2;
l_X1 = pTable.FindField(m_sX1);
l_Y1 = pTable.FindField(m_sY1);
l_X2 = pTable.FindField(m_sX2);
l_Y2 = pTable.FindField(m_sY2);
l_A1 = pTable.FindField(m_sAttrib1);
l_A2 = pTable.FindField(m_sAttrib2);
// Set up a Fields collection for the new Feature Class.
IFieldEdit pField , pFieldEdit;
IFieldsEdit pFields, pFieldsEdit;
IGeometryDefEdit pGeomDefEdit;
ISpatialReference pSR;
pFields = new FieldsClass();
pFieldsEdit = (IFieldsEdit)pFields;
pFieldsEdit.FieldCount_2 = 3;
// Create the geometry field.
pGeomDefEdit = new GeometryDefClass();
pSR = new UnknownCoordinateSystemClass();
pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
pGeomDefEdit.HasM_2 = false;
pGeomDefEdit.HasZ_2 = false;
pGeomDefEdit.SpatialReference_2 = pSR;
pFieldEdit = new FieldClass();
pFieldEdit.Name_2 = "Shape";
pFieldEdit.AliasName_2 = "Geometry";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
pFieldEdit.GeometryDef_2 = pGeomDefEdit;
//pFieldsEdit.Field(0) = pFieldEdit;
pFieldsEdit.set_Field(0, pFieldEdit);
// ' Set the two attribute Fields by cloning from the existing Table.
IClone pClone;
pClone = (IClone)pTable.Fields.get_Field(l_A1);
pFieldsEdit.set_Field(1,(IField)pClone.Clone());
pClone = (IClone)pTable.Fields.get_Field(l_A2);
pFieldsEdit.set_Field(2, (IField)pClone.Clone());
// Now create the new Shapefile. First create a Feature UID.
UID pCLSID;
pCLSID = new UIDClass ();
pCLSID.Value = "esricore.Feature";
IFeatureClass pFeatClass;
IWorkspace pWksp;
IFeatureWorkspace pFeatWksp;
IWorkspaceFactory pWkspFact;
pWkspFact = new ShapefileWorkspaceFactoryClass();
pFeatWksp = (IFeatureWorkspace)pWkspFact.OpenFromFile(@"path", 0);
pFeatClass = pFeatWksp.CreateFeatureClass("NameoftheFCtobecreated", pFields, pCLSID, null, esriFeatureType.esriFTSimple, "Shape", "");
// Now, create the Line data and add it to the new FeatureClass along with the
// specified attributes.
// If pFeatClass Is Nothing Then Exit Sub
int l_FCA1 , l_FCA2;
l_FCA1 = pFeatClass.FindField(m_sAttrib1);
l_FCA2 = pFeatClass.FindField(m_sAttrib2);
//Iterate all the rows in the selected Table.
ICursor pTableCursor ;
IRow pRow;
pTableCursor = pTable.Search(null, true);
//If pTableCursor Is Nothing Then Exit Sub
pRow = pTableCursor.NextRow();
IGeometryCollection pGeomColl;
ISegmentCollection pSegColl;
ILine pLine;
IPolyline pPolyline;
IFeature pFeat;
IPoint pFromPoint, pToPoint;
pTableCursor = pTable.Search(null, true);
//If pTableCursor Is Nothing Then Exit Sub
double X1, Y1, X2, Y2;
pRow = pTableCursor.NextRow();
//Do While Not pRow Is Nothing
while (pRow != null)
{
pFeat = pFeatClass.CreateFeature();
pFeat.set_Value(l_FCA1, pRow.get_Value(l_A1));
pFeat.set_Value(l_FCA2, pRow.get_Value(l_A2));
X1 = Convert.ToDouble(pRow.get_Value(l_X1));
Y1 = Convert.ToDouble(pRow.get_Value(l_Y1));
pRow = pTableCursor.NextRow();
X2 = Convert.ToDouble(pRow.get_Value(l_X1));
Y2 = Convert.ToDouble(pRow.get_Value(l_Y1));
pFromPoint = new PointClass();
pFromPoint.PutCoords(X1, Y1);
pToPoint = new PointClass();
pToPoint.PutCoords(X2, Y2);
//For each row in the Table, create a PolyLine.
pLine = CreateLn(pFromPoint, pToPoint);
pSegColl = new PathClass();
object Missing = Type.Missing;
pSegColl.AddSegment((ISegment)pLine, ref Missing , ref Missing);
pGeomColl = new PolylineClass();
pGeomColl.AddGeometry((IGeometry)pSegColl, ref Missing, ref Missing);
pFeat = pFeatClass.CreateFeature();
pPolyline = (IPolyline)pGeomColl;
//Set the Feature's Shape and the specified attributes.
pFeat.Shape = pPolyline;
pFeat.set_Value(l_FCA1, pRow.get_Value(l_A1));
pFeat.set_Value(l_FCA2, pRow.get_Value(l_A2));
pFeat.Store();
pRow = pTableCursor.NextRow();
}
}
Author of this article : Saud Ahmad, GIS Consultant, Houston,