English

How To: Programmatically get a count of features in a layer

Summary

This article shows how to use IFeatureClass::FeatureCount to return a count of features in the layer or the number of features that satisfy a query.

Procedure

  • Use Nothing in place of the Query Filter argument to return a count of all the features.

    Code:
    Sub FeatureCount()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument

    'Get the first layer in the map
    Dim pLayer As IFeatureLayer
    Set pLayer = pDoc.FocusMap.Layer(0)
    Dim pFc As IFeatureClass
    Set pFc = pLayer.FeatureClass

    'Get a count of all the features
    MsgBox pFc.FeatureCount(Nothing)

    End Sub

  • Use a Query Filter to return the features that satisfy the where clause.

    Code:
    Sub FeatureCount()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument

    'Get the first layer in the map
    Dim pLayer As IFeatureLayer
    Set pLayer = pDoc.FocusMap.Layer(0)
    Dim pFc As IFeatureClass
    Set pFc = pLayer.FeatureClass

    'Create a Query Filter
    Dim pQF As IQueryFilter
    Set pQF = New QueryFilter
    pQF.WhereClause = "STATE_NAME = 'California'"

    'Get a count of all the features
    MsgBox pFc.FeatureCount(pQF)

    End Sub