I need to get a value from an IObject.Fields.Field using ArcObjects 10.x using the criteria listed below:
If the field has a subtype associated with it, I need to get the subtype name/description based on the field value.
If the field has a coded value domain associated with it, I need to get the domain's name/description based on the field value.
If the field has neither a subtype or domain associated with it, I want the field value itself returned.
Having this functionality wrapped up in one call like "ReadFieldValue(IObject obj, int fieldIndex) is preferred.
Answer
Below is what I came up with:
///
/// Gets the field value from the IObject. Checks for coded
/// value domains, subtypes, or just the plain field value.
///
/// ESRI IObject
/// Field index for the IObject.Fields.Field
///
internal static object ReadFieldValue(IObject obj, int fieldIndex, bool ignoreDescription = false)
{
object fieldValue = null;
if (obj != null && fieldIndex > -1)
{
/// Default to the field value first,
/// overwrite it if a subtype or domain
/// is found.
fieldValue = obj.Value[fieldIndex];
if (fieldValue != DBNull.Value && ignoreDescription == false)
{
ISubtypes subtypes = obj.Class as ISubtypes;
IRowSubtypes rowSubtypes = obj as IRowSubtypes;
IField2 field = obj.Fields.Field[fieldIndex] as IField2;
// Set the appropriate domain based on the subtype if a subtype exists.
IDomain domain = null;
if (field != null)
{
if (subtypes != null && subtypes.SubtypeFieldIndex > -1)
{
domain = subtypes.get_Domain(rowSubtypes.SubtypeCode, field.Name);
}
else
{
domain = field.Domain;
}
}
// Check for a subtype field first
if (subtypes != null && rowSubtypes != null && subtypes.SubtypeFieldIndex == fieldIndex)
fieldValue = subtypes.get_SubtypeName(rowSubtypes.SubtypeCode);
// If it is not a subtype field, check for a domain field
else if (domain != null)
{
ICodedValueDomain2 cvDomain = domain as ICodedValueDomain2;
if (cvDomain != null)
{
for (int i = 0; i < cvDomain.CodeCount; i++)
{
if (cvDomain.get_Value(i).Equals(fieldValue))
{
fieldValue = cvDomain.get_Name(i);
break;
}
}
}
}
}
}
return fieldValue;
}
No comments:
Post a Comment