Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am looking for a way to get all selected features from a map which belongs to a specific feature class (shape file, 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);
}

Thanks a lot!

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 dont use them you've only the shape file included (default) and not the other fields and values.

Thank you all for your responses!

share|improve this question
    
What line in your code does the error occur on? –  artwork21 Feb 15 '13 at 19:46
    
pfeat.get_Value(x).ToString() ->Object reference not set to an instance of an object. –  FredFloete Feb 15 '13 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? –  egdetti Feb 15 '13 at 21:16
    
get_Value needs an integer for the index of the field. –  FredFloete Feb 15 '13 at 21:19
    
What value are you getting for x? –  egdetti Feb 15 '13 at 21:28
show 1 more comment

1 Answer 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);
  }
}
share|improve this answer
    
Thanks for the reply. Yes, for all elements in the feature class the field is set with a valid value. –  FredFloete Feb 15 '13 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 Feb 15 '13 at 20:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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