5

I am looking for a way to get all selected features from a map which belongs to a specific feature class (shapefile, geo database,…).

To be more specific and to give you a real case for my question: I want check if the feature class of selected feature has a special field. If this is the case I want to get the value of this field from the selected feature and add this value into a combobox. This process should be repeated for all selected features.

I tried the following but it crashed with an object reference error.

List<string> list = new List<string>();
IMap Pmap = doc.FocusMap;
IEnumFeature pEnumFeat = (IEnumFeature)Pmap.FeatureSelection;
pEnumFeat.Reset();
IFields fields;
try
{
  IFeature pfeat = pEnumFeat.Next();
  while (pfeat != null)
  {
    fields = pfeat.Fields;
    int x = fields.FindField("ID_K");
    if (!x.Equals(-1))
    {
      list.Add(pfeat.get_Value(x).ToString());
    }
    pfeat = pEnumFeat.Next();
    }
}
catch (Exception e)
{
  Console.WriteLine(e.Message);
}

UPDATE:

I've found a solution. Here is the working code:

List<string> list = new List<string>();
IMap Pmap = doc.FocusMap;
IEnumFeature pEnumFeat = (IEnumFeature)Pmap.FeatureSelection;
IFields fields;
pEnumFeat.Reset();
IEnumFeatureSetup enumFeatSetup = (IEnumFeatureSetup)pEnumFeat;
enumFeatSetup.AllFields = true;
try
{
   IFeature pfeat = pEnumFeat.Next();

   while (pfeat != null)
   {
      fields = pfeat.Fields;
      int x = fields.FindField("ID_K");
      list.Add(pfeat.get_Value(x).ToString());

      pfeat = pEnumFeat.Next();
    }
 }
 catch (Exception e)
 {
    Console.WriteLine(e.Message);
 }

These two line are especially important.

IEnumFeatureSetup enumFeatSetup = (IEnumFeatureSetup)pEnumFeat;
enumFeatSetup.AllFields = true;

If you don't use them you've only the shapefile included (default) and not the other fields and values.

6
  • What line in your code does the error occur on?
    – artwork21
    Commented Feb 15, 2013 at 19:46
  • pfeat.get_Value(x).ToString() ->Object reference not set to an instance of an object.
    – FredFloete
    Commented Feb 15, 2013 at 19:52
  • Just a thought, as I'm not very familiar with arcobjects, but is get_Value() looking for a field object and not just an index integer? So instead of get_Value(x) would get_Value(fields[x]) do anything?
    – bluefoot
    Commented Feb 15, 2013 at 21:16
  • get_Value needs an integer for the index of the field.
    – FredFloete
    Commented Feb 15, 2013 at 21:19
  • What value are you getting for x?
    – bluefoot
    Commented Feb 15, 2013 at 21:28

1 Answer 1

1

Are you sure the value of the field isn't null? What about:

if (!x.Equals(-1))
{
  object value = pfeat.get_Value(x);
  list.Add(value == null ? null : value.ToString());
}

I'm not sure if you want to inline the null test, you could always do:

if (!x.Equals(-1))
{
  object value = pfeat.get_Value(x);
  if(value != null)
  {
    list.Add(value.ToString());
  }
  else
  {
    list.Add(null);
  }
}
2
  • Thanks for the reply. Yes, for all elements in the feature class the field is set with a valid value.
    – FredFloete
    Commented Feb 15, 2013 at 19:56
  • Huh, I might be stumped then. From the error and the line number, there's a limited number of things that could be null. "list" certainly isn't, but I don't see how "pfeat" could be.
    – BDA
    Commented Feb 15, 2013 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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