Issue when add point to a IPointCollection

1292
2
10-05-2016 08:36 PM
WuYang
by
New Contributor II

Hi,

My polygon has several interior rings, and I want to move all vertices in these interior rings. I define the list of interior ring points into a list of IPointCollection, but when I try to add point to an IPointCollection, I got the "Index was out of range. Must be non-negative and less than the size of the collection" error.

My code as follows:

Dim polygon As IPolygon4 = TryCast(mFeature.Shape, IPolygon4)
Dim exteriorRingGeometryBag As IGeometryBag = polygon.ExteriorRingBag
Dim exteriorRingGeometryCollection As IGeometryCollection = TryCast(exteriorRingGeometryBag, IGeometryCollection)
Dim inRing As List(Of IPointCollection) = New List(Of IPointCollection)

For i = 0 To exteriorRingGeometryCollection.GeometryCount - 1
    Dim exteriorRingGeometry As IGeometry = exteriorRingGeometryCollection.Geometry(i)
    Dim exteriorRingPointCollection As IPointCollection = TryCast(exteriorRingGeometry, IPointCollection)
    Dim interiorRingGeometryBag As IGeometryBag = polygon.InteriorRingBag(TryCast(exteriorRingGeometry, IRing))
    Dim interiorRingGeometryCollection As IGeometryCollection = TryCast(interiorRingGeometryBag, IGeometryCollection)

    For k = 0 To interiorRingGeometryCollection.GeometryCount - 1
        Dim InteriorRingGeometry As IGeometry = interiorRingGeometryCollection.Geometry(k)
        Dim InteriorRingPointCollection As IPointCollection = TryCast(InteriorRingGeometry, IPointCollection)
        For m = 0 To InteriorRingPointCollection.PointCount - 1
            Dim pp As IPoint = InteriorRingPointCollection.Point(m)
            pp.X = Round(pp.X, 2)
            pp.Y = Round(pp.Y, 2)

           inRing(k).AddPoint(pp)  ' I got error this line

    Next
Next‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
2 Replies
ÁkosHalmai
Occasional Contributor II

There’s a variable called inRing, defined as a new List of IPointCollection, where the List is coming from the .NET’s Generic namespace. If you define such a list there is nothing inside, it is an empty container with zero instances in it. It’s not like a predefined array with exact number items. It’s totally empty. First, you should add an object (witch implements the IPointCollection interface) to the list with the inRing.Add method. This would be the 0th item in the list. After that you can add a point. However your solution seems complicated. Applying the IEnumVertex interface on the internal rings is probably easier.

Ákos Halmai

WuYang
by
New Contributor II

Hi Akos Halmai,

Thanks for your explanation, will follow you advice and update the result.

0 Kudos