I am trying to count the number of rows in a feature class attribute table. I have declared a feature cursor which i want to use later in the code to do some processing and field value calculation. (In the code below pFClass was declared to the IFeatureClass interface)

Dim pCursor As IFeatureCursor  
pCursor = pFClass.Search(Nothing, False)  

Dim pFeature As IFeature  
Set pFeature = pFCursor.NextFeature  

Dim LngCounter As Long  

Do Until pFeature Is Nothing  
    LngCounter = LngCounter + 1  
    Set pFeature = pCursor.NextFeature  

When i run this code to get the number of items in the table i use a msgbox to print the number to screen which is accurate. But then i Cannot reuse my pFeature because it is now nothing.

Other permutations i have tried include declaring 2 cursors and 2 IFeature objects and then using the first to get the total count and then the second to do subsequent calculations of the attribute table but to no avail. I looked at the ITable.rowCount but could not get that to work for me. I'd really appreciate some help on this.



Firstly, you really shouldn't start any new development in VBA right now. ArcGIS 10.0 is the last version which supports VBA.

Coming to your question, I would use IFeatureClass.FeatureCount Method. This directly give you the number of features that satisfy your query. If you pass Nothing for the query, it will return the total number of features in your featureclass. You need no loop over features, which is a slow process.

  • I know about vba going out but thanks for pointing it out. I'm not using any specific query. Is there no way to just count all the items in the attribute table of a feature class without using a query. – goonerify Mar 11 '13 at 16:43
  • 1
    Why do you want to make things harder than they need to be? Don't use VBA, don't use cursors when you don't need to. If you really want to reuse the cursor for this, then call its Reset method. – blah238 Mar 11 '13 at 17:40
  • @IQ_HiGh just Pass Nothing in the method to get the count of all features in your featureclass. – Devdatta Tengshe Mar 12 '13 at 3:18

When I need features later in my code, what I always do is create a list of features from the feature cursor. As I have a very little idea about VBA, I am posting the code in c#. I hope you can convert it if you really want to use VBA.

List<IFeature> GetAllFeature(IFeatureClass featureClass)
    List<IFeature> features = new List<IFeature>();

    IFeatureCursor featureCursor = featureClass.Search(null, false);

    IFeature feature;
    while ((feature = featureCursor.NextFeature()) != null)
    return features;

This code returns all features of the feature class.

Now if you need to do anything with features later, you can do that. and to get the count simply use features.Count. As you said you wanted to reuse your pFeature, so I recommend this process. But if you need the count of the features only, then you can use featureClass.FeatureCount(null) as Devdatta Tengshe said. This also give you the actual count of all features of the feature class

  • 1
    You don't have lists and generic types in VBA. – Devdatta Tengshe Mar 12 '13 at 3:01
  • Actually I don't have much idea about VBA. But there should be something equivalent list in VBA. Doesn't it? – Emi Mar 12 '13 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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