Using a nested loop

447
2
10-17-2011 10:45 PM
DemetrisDemetriou
New Contributor
I use the following nested loops to read the first point feature of a dataset and the rest points in that dataset. Then read the second point feature of a dataset and the rest points in that dataset and so on. The problem with these nested loops is that it reads the first point and the rest points only once. Why this happens?


    Dim pFCursor As IFeatureCursor
    Set pFCursor = pFeatureClass.Search(Nothing, True)

    Dim pFCursor2 As IFeatureCursor
    Set pFCursor2 = pFeatureClass.Search(Nothing, True)


    Dim pFeature As IFeature
    Dim pFeature2 As IFeature

    Dim pPointA As IPoint
    Dim pPointB As IPoint


    Set pFeature = pFCursor.NextFeature
    Do Until pFeature Is Nothing
   
    Set pPointA = pFeature.Shape

    MsgBox pFeature.Value(intPosFID) & "F1"

       Set pFeature2 = pFCursor2.NextFeature
       Do Until pFeature2 Is Nothing


             Set pPointB = pFeature2.Shape

             MsgBox pFeature2.Value(intPosFID) & "F2"


       Set pFeature2 = pFCursor2.NextFeature
       Loop


      Set pFeature = pFCursor.NextFeature
       Loop
0 Kudos
2 Replies
DuncanHornby
MVP Notable Contributor
I think you need to reset the second feature cursor with each loop of your first feature cursor.  You code should look like:

Dim pFCursor As IFeatureCursor
Set pFCursor = pFeatureClass.Search(Nothing, True)
Dim pFCursor2 As IFeatureCursor
Dim pFeature As IFeature
Dim pFeature2 As IFeature
Dim pPointA As IPoint
Dim pPointB As IPoint

Set pFeature = pFCursor.NextFeature
Do Until pFeature Is Nothing

    Set pPointA = pFeature.Shape
    MsgBox pFeature.Value(intPosFID) & "F1"
    Set pFCursor2 = pFeatureClass.Search(Nothing, True)
    Set pFeature2 = pFCursor2.NextFeature
    Do Until pFeature2 Is Nothing
        Set pPointB = pFeature2.Shape
        MsgBox pFeature2.Value(intPosFID) & "F2"
        Set pFeature2 = pFCursor2.NextFeature
    Loop
    Set pFeature = pFCursor.NextFeature
Loop


Duncan

p.s. Please place your code samples inside the code tags (the # symbol on the toolbar)
0 Kudos
DemetrisDemetriou
New Contributor
Yes, Duncan. You are right! That was the mistake. Thank you very much.
Demetris
0 Kudos