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?
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?