1

I have been able to get past this error, but I don't know why it was happening.

I'm attempting to set symbology on a layer using IUniqueValueRenderer in ArcMap 10.4. When I try and use the AddValue method I receive the 'object does not contain a definition for 'AddValue'.

This error is encountered at runtime.

var uvRenderer = new ESRI.ArcGIS.Carto.UniqueValueRendererClass() as IUniqueValueRenderer;
uvRenderer.FieldCount = 1;
uvRenderer.set_Field(0, "SampleField");
uvRenderer.DefaultSymbol = graySymbol as ISymbol; //a symbol for a gray bordered polygon with no fill
uvRenderer.UseDefaultSymbol = true;

int idIndex = SampleAreasFC.Fields.FindField("SampleField");

if (idIndex != -1)
    //the error occurs on this line. SampleAreaFeature.get_Value(idIndex) returns a non-null value.
    uvRenderer.AddValue(SampleAreaFeature.get_Value(idIndex).ToString(), "SampleField", redSymbol as ISymbol);

The way to fix this error was to do this instead:

 string value = SampleAreaFeature.get_Value(idIndex).ToString();
 uvRenderer.AddValue(value, "SampleField", redSymbol as ISymbol);

Even more strange was while that worked, the following generated the same error as before:

 var value = SampleAreaFeature.get_Value(idIndex).ToString();
 uvRenderer.AddValue(value, "SampleField", redSymbol as ISymbol);

Since var is just an implicit type, it should be functionally identical to the former, but still generated the same error as before.

Does anyone know what would cause this behavior?

UPDATE:

I also asked this question on StackOverflow because I thought there was a chance this may be a more general .NET issue rather than specifically ArcObjects. That question is here. However, that is not resolved either.

This isn't really the ArcObjects question I initially thought, but it may still be useful to leave up.

The core issue is that get_value returns a dynamic type and that shadows the ToString method making my 'var' declaration on 'value' be a dynamic type. The issue is then that the .NET RuntimeBinder isn't able to find AddValue at runtime.

In this case get_value returns an integer and ToString correctly gives me the string "347". AddValue can be found via reflection at runtime, so it seems like there may be an issue with finding the function on the associated COM object when passing a dynamically typed variable.

So the more specific question is now, why can't RuntimeBinder find AddValue when a valid string is passed in as a dynamic variable?

  • Why are you using var? If you specify the object as IUniqueValueRenderer uvRenderer = new ESRI.ArcGIS.Carto.UniqueValueRendererClass(); then you shouldn't have problems with the objects' methods. – Michael Stimson Apr 11 '18 at 22:59
  • Using var with UniqueValueRenderer isn't the problem, var for value is the issue. I need to update this question with more details as it seems to only be tangentially related to ArcObjects. – danielm Apr 12 '18 at 14:35
  • If get_Value is indeed the issue, would it help to declare a separate (string) variable holding the value? Or maybe use SampleAreaFeature.Value[idIndex]? – Berend Apr 12 '18 at 14:58
  • Explicitly declaring value as a string (like in my second code block) works without issue. IFeature.Value[int] is just a shortcut to get_Value. The bigger question is why AddValue cannot be found at runtime when the string type isn't explicitly declared. – danielm Apr 12 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.