2

I am trying to access the domain of a selected feature. I have seen examples for VB.NET and C#.NET, but I cannot access the domain of a specified field.

Dim findName As String = Nothing
Dim findValue As Integer = 21 'the domain value for AGRICULTURE
Dim fieldIndex As Integer = m_Feature.Fields.Field("my_field") 'this has a coded domain

'--Check for my_field
If fieldIndex <> -1 Then
    Dim domain As IDomain = m_Feature.Field(fieldIndex).Domain
    If domain.Type = esriDomainType.esriDTCodedValue Then
        Dim cvDomain As ICodedValueDomain2 = TryCast(domain, ICodedValueDomain2)
        For i = 0 To cvDomain.CodeCount - 1
            If cvDomain.Value(i).Equals(findValue) Then
                findName = cvDomain.Name(i)
                Exit For
            End If
        Next i
    End If

When debugging it hangs up on the line with domain.Type, and it shows that the value of Domain is Nothing. The documentation shows that IField.Domain should return the default domain as an IDomain object. I know the field has a domain, but I don't know if it is default and am unable to change it. Does anyone have any advice on how I should access the domain of a field (of a selected feature in my case)?

  • Are you sure the field has a domain associated? Domains can also be specified only for a particular subtype. – Petr Krebs Aug 24 '15 at 21:29
  • @PetrKrebs The featureclass has subtypes, and the field in question can have different domains depending on subtype. – Barbarossa Aug 24 '15 at 21:57
  • Right.. so you need to retrieve the domain for a specific field and a specific subtype (see resources.arcgis.com/en/help/arcobjects-net/componenthelp/…). The default domain will probably not make much sense in that case. – Petr Krebs Aug 24 '15 at 23:07
0

Thanks to Petr's comments, I found the solution. Below is my functioning code.

Dim findName As String = Nothing
Dim findValue As Integer = 21 'the domain value for AGRICULTURE
Dim fieldIndex As Integer = m_Feature.Fields.Field("my_field") 'this has a coded domain
Dim subfieldIndex As Integer = m_Feature.Fields.FindField("subfield") 'index of subtype field
Dim subfield As Integer = m_Feature.Value(subfieldIndex) 'value of subtype field  

'--Check for my_field
If fieldIndex <> -1 Then

    Dim subtypes As ISubtypes = TryCast(m_Feature.Class, ISubtypes)
    Dim domain As IDomain = subtypes.Domain(subfield, "my_field")

    If domain.Type = esriDomainType.esriDTCodedValue Then
        Dim cvDomain As ICodedValueDomain2 = TryCast(domain, ICodedValueDomain2)
        For i = 0 To cvDomain.CodeCount - 1
            If cvDomain.Value(i).Equals(findValue) Then
                findName = cvDomain.Name(i)
                Exit For
            End If
        Next i
    End If
  • 1
    Don't forget to mark as the answer – Midavalo May 18 '17 at 15:28

Your Answer

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

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