arcobjects

get_value of a feature in IFeatureCursor


I'm trying to read the attribute "POSTCODE" of the features in IFeatureCursor. The FID was successful read but the "POSTCODE" was failed. The runtime error 'An expected Field was not found or could not be retrieved properly. Appreciate your advise. Paul

   private void test2(IFeatureCursor pFeatc1)
    {
        IFeature feature = null;
        IFields pFields;
        int ctcur = 0;
        while ((feature = pFeatc1.NextFeature()) != null)
        {
            pFields = feature.Fields;
            int indxid = pFields.FindField("FID");
            int indxpost = pFields.FindField("POSTCODE");
            object valu = feature.get_Value(indxid);
            string valupost = feature.get_Value(indxpost);
            string aValu = Convert.ToString(valu);
            Debug.WriteLine("FID: " + aValu + " Postcode: " + valupost);
            ctcur++;
            feature = pFeatc1.NextFeature();
        }
        MessageBox.Show("count cursor = " + ctcur);
    }

Solution

  • I have modified the program and successfully read the feature attribute 'POSTCODE'. I have added IFeatureClass.Search(queryFilter, true) to search the feature again by FID and save in a cursor then use the 'feature.get_Value' to read the attribute. Please see my updated code below. Thanks.

       private void test2(IFeatureCursor pFeatc1)
        {
            IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
            IMap map = mxdoc.FocusMap;
            IFeatureLayer flayer;
            IMaps pMaps = mxdoc.Maps;
            for (int i = 0; i <= pMaps.Count - 1; i++)
            {
                map = pMaps.get_Item(i);
                IEnumLayer pEnumLayer = map.get_Layers(null, true);
                pEnumLayer.Reset();
                ILayer pLayer = pEnumLayer.Next();
                while (pLayer != null)
                {
                    if (pLayer.Name == "AddrKey")
                    {
                        Debug.WriteLine("Layer: " + pLayer.Name);
                        flayer = (IFeatureLayer)pLayer;
                        IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
                        IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
    
                        IFeature feature = null;
                        IFields pFields;
                        while ((feature = pFeatc1.NextFeature()) != null)
                        {
                            pFields = feature.Fields;
                            int indx = pFields.FindField("FID");
                            object valu = feature.get_Value(indx);
                            string sFID = Convert.ToString(valu);
                            IQueryFilter queryFilter = new QueryFilter();
                            queryFilter.WhereClause = ("FID = " + sFID);
                            Debug.WriteLine("FID: " + sFID);
                            queryFilter.SubFields = "POSTCODE";
                            int fieldPosition = pFeatureClass.FindField("POSTCODE");
                            IFeatureCursor featureCursor = pFeatureClass.Search(queryFilter, true);
                            while ((feature = featureCursor.NextFeature()) != null)
                            {
                                MessageBox.Show(feature.get_Value(fieldPosition));
                            }
                            feature = pFeatc1.NextFeature();
                        }
                    }
                   pLayer = pEnumLayer.Next();
                }
            }
        }