Convert COM to Non-COM Object

533
1
03-11-2011 11:41 AM
BillMacPherson
New Contributor III
In a .net web service that I'm writing, I'm having trouble converting from a COM to non COM object. The destination object is an ArcServer geometry service polygonN. The source is an IFeature. Is there some intermediate step to go through?



   Private Sub FindShape(ByRef pPolygon As my_geometry_service.PolygonN, _
                          ByVal pFeature As IFeature)

        Try
            If pFeature Is Nothing Then
                Throw New Exception("Input feature is nothing.")
            End If


            pPolygon = CType(pFeature.Shape, my_geometry_service.PolygonN) ' *** error ***


        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub



Error Message:
Unable to cast COM object of type 'System.__ComObject' to class type 'MyProject.my_geomety_service.PolygonN'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

Thanks in advance for any help with this.
Bill
0 Kudos
1 Reply
BillMacPherson
New Contributor III
I found something that appears to work.

' Reference: Converting between data types
'[URL="' http://resources.esri.com/help/9.3/arcgisserver/adf/dotnet/concepts_start.htm#developer/ADF/conversi..."]

' Convert COM polygon to SOAP polygon
Dim t As Type = GetType(my_geometry_service.PolygonN)
Dim pPoly As mycity_geometry.PolygonN
pPoly = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ComObjectToValueObject(pFeature.Shape, servercontext, t)

pPoly.SpatialReference.WKID = 102740 ' For State Plane 4204 projection
Debug.Print(pPoly.SpatialReference.WKID.ToString)
0 Kudos