How to create attribute domains


Summary
This article shows how to gain access to the workspace domain from a workspace. The process of creating and adding to the workspace of a range and coded value domain is also discussed. Additionally, how to print the valid values of a domain to a console window is shown.

In this topic


Domains

A domain is an abstract class that defines an interface used by RangeDomain and CodedValueDomain coclasses to constrain the permissible values that can be associated with a particular field on an object or feature class. Domains are associated with a field object and can be at the feature class or subtype level.
The ability to assign domains at the subtype level means you can create attribute domains for different types of features and apply them to the same field only if the specific type of feature is created. If a field ever has domains assigned at both levels, the domain defined on the subtype will be used, however this workflow is not recommended.
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 within the geodatabase.
A domain can be shared by any number of fields. Domains are stored at the workspace level and are added to the workspace through IWorkspaceDomains.AddDomain.
IDomain
The IDomain interface provides access to the common properties shared across both types of domains. Each of the properties is read/write except for the Type property. When creating and assigning a domain to a field, the client is required to set the Name and FieldType properties.
RangeDomains
RangeDomains can be associated with either numeric fields (such as esriFieldTypeSmallInteger or esriFieldTypeDouble) or data fields. RangeDomains may not be associated with string or character fields (esriFieldTypeString).
IRangeDomain
The IRangeDomain interface allows the client to examine the minimum and maximum range values of an existing object or set the range values in a new RangeDomain that they are in the process of creating. 
The following code shows the creation of a new range domain for a double field. The range domain is then persisted in the geodatabase when it is added to the workspace.
[Java]
// For example, domainName = "RGB Value"
// minValue = 0
// maxValue = 255
static void addRangeDomain(IWorkspace workspace, String domainName, Double minValue,
    Double maxValue)throws Exception{
    // Cast the workspace to the IWorkspaceDomains interface.
    IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;
    // The code to create a new range domain.
    IRangeDomain rangeDomain = new RangeDomain();
    rangeDomain.setMinValue(minValue);
    rangeDomain.setMaxValue(maxValue);
    // The code to set the common properties for the new range domain.
    IDomain domain = (IDomain)rangeDomain;
    domain.setName(domainName);
    domain.setFieldType(esriFieldType.esriFieldTypeInteger);
    domain.setSplitPolicy(esriSplitPolicyType.esriSPTDuplicate);
    domain.setMergePolicy(esriMergePolicyType.esriMPTAreaWeighted);
    // Add the new domain to the workspace.
    workspaceDomains.addDomain(domain);
}
CodedValueDomains
CodedValueDomains store a set of value and name pairs that represent the discrete values that a field can take. The value is what is persisted inside a field, and the name is displayed by the ArcMap property inspector. The name can be considered a human-readable string that describes what the value represents. In contrast to RangeDomains, CodedValueDomains can also be associated with string fields (esriFieldTypeString) and the value can be a string.
ICodedValueDomain
The ICodedValueDomain interface provides the mechanism for adding and removing the value and name pairs from a CodedValueDomain. In addition, it provides properties that allow users to examine the value and name pairs on an index basis. This index value must be between 0 and (CodeCount – 1) or an error is returned.
The following code shows the creation of a new range domain for a double field. The coded value domain is then persisted in the geodatabase when it is added to the workspace.
[Java]
static void addBuildingTypeCodedValueDomain(IWorkspace workspace)throws Exception{
    // Cast the workspace to the IWorkspaceDomains interface.
    IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;
    // The code to create a new coded value domain.
    ICodedValueDomain codedValueDomain = new CodedValueDomain();
    // Value and name pairs.
    codedValueDomain.addCode("RES", "Residential");
    codedValueDomain.addCode("COM", "Commercial");
    codedValueDomain.addCode("IND", "Industrial");
    codedValueDomain.addCode("BLD", "Building");
    // To remove a value from an existing coded value domain:
    codedValueDomain.deleteCode("BLD"); // The "Building" code is removed.
    // The code to set the common properties for the new coded value domain.
    IDomain domain = (IDomain)codedValueDomain;
    domain.setName("Building Types");
    domain.setFieldType(esriFieldType.esriFieldTypeString);
    domain.setSplitPolicy(esriSplitPolicyType.esriSPTDuplicate);
    domain.setMergePolicy(esriMergePolicyType.esriMPTDefaultValue);
    //Add the new domain to the workspace.
    workspaceDomains.addDomain(domain);
}
Printing domain values to the console
To print the values of a domain, you must first get a reference to a domain to pass into the function. In the following code example, the IWorkspaceDomains.DomainByName is used to get a domain from the workspace.
[Java]
static IDomain getDomainByName(IWorkspace workspace, String domainName)throws
    Exception{
    // Cast the workspace to the IWorkspaceDomains interface.
    IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;
    // Get the domain and return it.
    IDomain requestedDomain = workspaceDomains.getDomainByName(domainName);
    return requestedDomain;
}
The following function takes a domain and prints the valid values to the console. This functionality could also be used to populate a drop-down list in a custom editing application.
[Java]
static void printDomainValidValues(IDomain domain)throws Exception{
    ICodedValueDomain codedValueDomain;
    IRangeDomain rangeDomain;
    String validValues = "\n";
    if (domain != null){
        if (domain.getType() == esriDomainType.esriDTCodedValue){
            codedValueDomain = (ICodedValueDomain)domain;
            for (int i = 0; i < codedValueDomain.getCodeCount(); i++){
                // codedValueDomain.getValue(i) returns the code that is stored in the geodatabase (e.g, RES).
                //  codedValueDomain.getName(i) returns the name that is shown in the ArcMap inspector (e.g., Residential).

                validValues = validValues + codedValueDomain.getValue(i) + "\n";
            }
        }
        else{
            rangeDomain = (IRangeDomain)domain;
            validValues = "all values >= to " + rangeDomain.getMinValue() + 
                " and <= to " + rangeDomain.getMaxValue();
        }
        System.out.println("The valid values for " + domain.getName() + " are " +
            validValues);
    }
}


See Also:

How to create subtypes




Additional Requirements
  • If working in ArcSDE, an ArcEditor or greater license is needed on 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