[GIS] Creating feature class in memory and adding to map using ArcObjects

arcgis-9.3arcobjectsvba

I have the code below, which is supposed to create a new featureclass in memory then add the FC to the map, but I get a Automation error when I use the createfeatureclass method

Set pFC = pFeatWS.CreateFeatureClass("NewBus", pFields, Nothing,
Nothing, esriFTSimple, "Shape", " ")

Here' s the full code

Public Sub InMemoryWorkspace()

'Create Workspace factory
Dim pWorkspaceFactory As IWorkspaceFactory2
Set pWorkspaceFactory = New InMemoryWorkspaceFactory

'Create workspace
Dim pWorkspaceName As IWorkspaceName2
Set pWorkspaceName = pWorkspaceFactory.Create("", "MyInMemoryworkspace", Nothing, 0)

'Get a handle on Name
Dim PName As IName
Set PName = pWorkspaceName

'Get handle on Featureworkspace
Dim pFeatWS As IFeatureWorkspace
Set pFeatWS = PName.Open

'Create field object and set number of fields
Dim pFields As IFields
Set pFields = New Fields
Dim pFieldsEdit As IFieldsEdit
Set pFieldsEdit = pFields
pFieldsEdit.FieldCount = 3

'Create objectID field
Dim pField As IField
Set pField = New Field
Dim pFieldEdit As IFieldEdit
Set pFieldEdit = pField
With pFieldEdit
    .Name = "OBJECTID"
    .Type = esriFieldTypeOID
End With
Set pFieldsEdit.Field(0) = pField

'Create postcode field

Set pField = New Field

Set pFieldEdit = pField
With pFieldEdit
    .Name = "POSTCODE"
    .Type = esriFieldTypeString
End With
Set pFieldsEdit.Field(1) = pField

'Create Shape field
Set pField = New Field
Set pFieldEdit = pField
With pFieldEdit
    .Name = "SHAPE"
    .Type = esriFieldTypeGeometry
End With
Set pFieldsEdit.Field(2) = pField


Dim pGeomDef As IGeometryDef
Dim pGeomDefEdit As IGeometryDefEdit
Set pGeomDef = New GeometryDef
Set pGeomDefEdit = pGeomDef
With pGeomDefEdit
  .GeometryType = esriGeometryPolygon
  Set .SpatialReference = New UnknownCoordinateSystem
End With
Set pFieldEdit.GeometryDef = pGeomDef
pFieldsEdit.AddField pField

' Create featureclass
 Dim pFC As IFeatureClass
 Set pFC = pFeatWS.CreateFeatureClass("NewBus", pFields, Nothing, Nothing, esriFTSimple, "Shape", " ")

 Dim pmemLayer As IFeatureLayer
 Set pmemLayer.featureClass = pFC

 Dim pmxdoc As IMxDocument
 Set pmxdoc = ThisDocument
 Dim pmap As IMap
 Set pmap = pmxdoc.FocusMap

 pmap.AddLayer pFC

Can someone explain where I am going wrong?

Best Answer

Try calling SetDomain on the spatial reference. See code here.

Related Question