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
LOOP
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.
2 Answers 2
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.
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)
features.Add(feature);
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
Explore related questions
See similar questions with these tags.