2

I call Delete() on a feature in a feature class, then I want to proceed to the next feature by using ICursor.NextRow() but this produces an error. The code is simple: EDIT: More code showing the instantiation of the cursor and the version is ArcGIS 10.2

 ICursor curs;
 object id;
 IFeature feature;
 curs = (ICursor)fclass.Search(new QueryFilter(), true);
 feature = (IFeature)curs.NextRow();
 if(some_criteria){

 while (feature != null)
        {
            try
            {

                id = feature.get_Value(index); //get the ID of the record which is not in source
                if (id does not exist in target)
                {
                    feature.Delete();
                }
                feature = (IFeature)curs.NextRow();
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);//testing purposes
            }
        }
        curs = null;
    }
}

Now, the deletion occurs, but in the next iteration, I get an error at the line: id=feature.get_Value(index) saying "Feature was deleted". My question is, shouldn't reassigning feature to the next row be good enough to move on?

closed as unclear what you're asking by PolyGeo, Brad Nesom, Paul, Fezter, Vince Jul 1 '14 at 1:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    How about a larger chunk of code, so we can see how all the objects were instantiated? It would also help to specify the version of ArcGIS in use. – Vince Jun 30 '14 at 20:13
  • 2
    It sounds like you're using a recycling cursor, can you include the part about the creation of the cursor. Note: curs = null; is not the way to get rid of the cursor object use ReleaseComObject to close the cursor and remove locks. – Michael Stimson Jun 30 '14 at 21:39
7

The method for removing a feature depends on the cursor.

If you are using a recycling cursor: pCur = pFeatClass.Search(null,true) do not call delete in any way, shape or form. Calling delete on a recycling cursor removes the buffer so there is nothing to load the next feature into which is why it's getting cranky when you call NextFeature() - you've already broken the buffer!

In order to delete a feature on a search cursor:

IFeatureCursor pCur = pFeatClass.Search(pQF,false); // not recycling
IFeature pFeat = pCur.NextFeature();
do
{
  pFeat.Delete();
  pFeat = pCur.NextFeature();
} while (pFeat != null);

To delete a feature on an update cursor:

IFeatureCursor pCur = pFeatClass.Update(pQF,false); // not recycling
IFeature pFeat = pCur.NextFeature();
do
{ 
  pCur.DeleteFeature(); // not called on the feature directly
  pFeat = pCur.NextFeature();
} while (pFeat != null);

To dismiss the cursor use ReleaseComObject:

System.Runtime.InteropServices.Marshal.ReleaseComObject(pCur);

If you want to delete a few features using a query filter then use the ITable interface:

ITable pTable = (ITable) pFeatClass; // ITable inherits from IFeatureClass
pTable.DeleteSearchedRows(pQF); // delete every row that fits the query filter

This is a much safer way to remove a lot of features that satisfy the query. Note: if the query filter is null then it will remove ALL ROWS (features). I'm not sure if it would work if you used a spatial filter on an ITable object but I wouldn't try it!

Not the answer you're looking for? Browse other questions tagged or ask your own question.