How to assign domains to fields


Summary
Domains are assigned to a field in an object or feature class. The method to assign a domain to a field depends on whether the object or feature class has subtypes. If there are subtypes, you can further refine the assignment to the subtype level. This article shows the workflow for both methods of domain assignment.

In this topic


Assigning domains to fields

Domains are used to constrain the permissible values that can be associated with a particular field in an object or feature class. Domains are used by the ArcMap property inspector to constrain the values that can be typed in a field, as well as during the validation process in the geodatabase.
Domains are associated with a field object and are stored at the workspace level. This allows you to assign the same domain to multiple fields across objects or feature classes (from this point forward, only feature classes will be discussed, but both apply) as long as the field types match.
When assigning a domain to a field in a feature class, determine if the feature class has subtypes or not.
 
The same workflow is required to assign the domain independent of its domain type (range or coded value).
Assigning domains to fields in an object or feature class without defined subtypes  
The first case discussed is if the feature class does not have subtypes defined. In this case, the domain will be assigned directly to the field through the IClassSchemaEdit interface.
The example to assign a domain to a field has three arguments:
The code example verifies that a feature class was supplied and that the domain and field both exist. It also verifies that the domain and field have the same field type. If this is true, the domain is assigned with a call to AlterDomain supplying the name of the field and the domain to be assigned.
The following code example assigns a domain to a field in a feature class without defined subtypes:
If a field has domains assigned at both the feature class and subtype level, the subtypes domain will be used; however, this is not recommended and should be avoided.
[Java]
// For example; nameOfDomain = "Material".
// nameOfField = "CP_MATERIAL".
static void assignDomainToField(IFeatureClass featureClass, String domainName,
    String fieldName)throws Exception{
    if (featureClass == null){
        System.out.println("No feature class was passed into the function");
        return ;
    }
    // Cast from feature class to IClassSchemaEdit.
    IClassSchemaEdit3 classSchemaEdit = new IClassSchemaEdit3Proxy(featureClass);
    // Cast for IDataset.
    IDataset dataset = new IDatasetProxy(featureClass);
    // Use the dataset to get the workspace, cast for IWorkspaceDomains.
    IWorkspaceDomains workspaceDomains = new IWorkspaceDomainsProxy
        (dataset.getWorkspace());
    // Use workspaceDomains to get the domain you will be assigning to the field.
    IDomain domain = workspaceDomains.getDomainByName(domainName);
    // The following three if statements are not nessecary, but can be used for debuging purposes.
    if (domain == null)
    { // check if the domain exists, otherwise exit
        System.out.println("The domain named " + domainName + 
            " was not found in the workspace");
        return ;
    }
    // Get the field you will be assigning the domain to.
    IField field = featureClass.getFields().getField(featureClass.getFields()
        .findField(fieldName));
    if (field == null)
    { //Verify if the field exists in the feature class, otherwise exit.
        System.out.println("The field named " + fieldName + 
            " was not found in the feature class");
        return ;
    }
    if (field.getType() == domain.getFieldType())
    { // check the field and domain have the same field type.
        // If they are the same, use IClassSchemaEdit::AlterDomain to assign the domain to the field.
        classSchemaEdit.alterDomain(fieldName, domain);
    }
    else{
        System.out.println(
            "The field and the domain have different field types, Field = " +
            field.getType() + ", Domain = " + domain.getFieldType());
        return ;
    }
}
Assigning domains to fields in an object or feature class with defined subtypes
The second case discussed is if the feature class does have defined subtypes. In this case, the domain will be assigned to the field through the ISubtypes interface.
For this example, you need to know some background information about the field and the feature class that you are assigning the domain to. Assume that you have a polyline feature class that is being used to model pipes. The feature class has a field named TYPECODE that is used to define the subtypes. Currently, only three subtypes are defined:
The geodatabase that the feature class is stored in has domains for the diameters of two of the pipe types. The coded value domains are named DistDiam and TransDiam. The field that you will be applying the subtypes to is named SIZE_ONE.
The code example accepts the feature class and casts to the ISubtypes interface. Both domains are retrieved from the workspace by name and assigned to the SIZE_ONE field through the subtype code they are to be associated with.  It is not necessary to assign domains (or default values) to all subtypes on any given field.
The following code example assigns a domain to a field in a feature class with defined subtypes:
[Java]
static void assignDomainToFieldWithSubtypes(IFeatureClass featureClass)throws
    Exception{
    // Cast for ISubtypes.
    ISubtypes subtypes = new ISubtypesProxy(featureClass);
    // Cast for IDataset.
    IDataset dataset = new IDatasetProxy(featureClass);
    // use the dataset to get the workspace, cast for IWorkspaceDomains
    IWorkspaceDomains workspaceDomains = new IWorkspaceDomainsProxy
        (dataset.getWorkspace());
    // Get the domains from the workspace.
    IDomain highwayMaterialDomain = workspaceDomains.getDomainByName("DistDiam");
    IDomain secondaryMaterialDomain = workspaceDomains.getDomainByName("TransDiam");
    // Assign the domain to the SIZE_ONE field on the distribution main subtype.
    subtypes.setDomainByRef(1, "SIZE_ONE", highwayMaterialDomain);
    // Assign the domain to the SIZE_ONE field on the transmission main subtype
    subtypes.setDomainByRef(2, "SIZE_ONE", secondaryMaterialDomain);
}


See Also:

How to create attribute domains
How to create subtypes




Additional Requirements
  • If working in ArcSDE, an ArcEditor or greater license is required in ArcGIS Desktop, and the Geodatabase Update extension is required for ArcGIS Engine.

Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine: Geodatabase Update