Feature's HasOID is false, values are empty

4115
2
Jump to solution
06-02-2015 08:08 AM
DavidStefan
New Contributor III

I'm trying to get a value of a particular field from selected features. The features are from a layer that contains data from a file geodatabase. The data has been imported from an excel spreasheet, the file geodatabase is set as the default geodatabase and the feature class appears to be registered with it (the "Register with Geodatabase" option in the Manage menu is disabled).

I obtain IEnumFeature from the focused map's FeatureSelection:

IMap map = ArcMap.Document.ActiveView.FocusMap;
IEnumFeature ftEnum = map.FeatureSelection as IEnumFeature;

I then have a while loop that fetches selected features from the enum and tries to get a particular field value:

IFeature ft = null;

while ((ft = ftEnum.Next()) != null) {
    int fid = ft.Fields.FindField("LOCODE");
    string locode = ft.get_Value(fid) as string;
}

Now, the fid gets correctly assigned to 1, which is the index of the "LOCODE" field, but the returned value is null. I noticed that the features do not have OID associated with them. Why is that? If I export the feature class as a shapefile and add it to the map, the OIDs are set. But the "LOCODE" values are missing in either case.

Why is this happening?

0 Kudos
1 Solution

Accepted Solutions
DavidStefan
New Contributor III

OK, here's a solution. Looping over the IEnumFeature doesn't ensure that all fields are returned, apparently. In my case this resulted in NO field values being returned, including the OID which was missing.

I have managed to get the fields by setting the AllFields of the IEnumFeatureSetup to true. Code below illustrates the solution, hope this helps someone.

IMxDocument doc = ArcMap.Application.Document as IMxDocument;
IMap map = doc.FocusMap;

// Retrieve selected features enum
IEnumFeature ftEnum = map.FeatureSelection as IEnumFeature;
IEnumFeatureSetup ftSetup = (IEnumFeatureSetup)ftEnum;

// Make sure ALL fields are returned
ftSetup.AllFields = true;

ftEnum.Reset();
IFeature ft;

while ((ft = ftEnum.Next()) != null) {
    int fldID = ft.Fields.FindField("LOCODE");
    message += ft.get_Value(fldID) + "\n";
}

View solution in original post

0 Kudos
2 Replies
DavidStefan
New Contributor III

OK, here's a solution. Looping over the IEnumFeature doesn't ensure that all fields are returned, apparently. In my case this resulted in NO field values being returned, including the OID which was missing.

I have managed to get the fields by setting the AllFields of the IEnumFeatureSetup to true. Code below illustrates the solution, hope this helps someone.

IMxDocument doc = ArcMap.Application.Document as IMxDocument;
IMap map = doc.FocusMap;

// Retrieve selected features enum
IEnumFeature ftEnum = map.FeatureSelection as IEnumFeature;
IEnumFeatureSetup ftSetup = (IEnumFeatureSetup)ftEnum;

// Make sure ALL fields are returned
ftSetup.AllFields = true;

ftEnum.Reset();
IFeature ft;

while ((ft = ftEnum.Next()) != null) {
    int fldID = ft.Fields.FindField("LOCODE");
    message += ft.get_Value(fldID) + "\n";
}
0 Kudos
TedRakel
New Contributor III

Thanks for sharing this information.  I had the exact same problem.

0 Kudos