This document is archived and information here might be outdated.  Recommended version.


Validating features (ArcObjects .NET 10.4 SDK)

Validating features


Summary
This topic discusses how to use the IValidation and IValidate interfaces to validate the attributes, relationships, and connectivity of an object class.

In this topic


About validating features

Validation is the process of verifying an object against any rules that are defined for the object's class and subtype to determine if any of the rules have been violated. Validation can be performed on an entire dataset, a subset of a dataset (defined by a query filter, set, or selection set), or an individual feature. The validation of an object includes the following items:
  • Validating the subtype value
  • Validating the attribute rules
  • Validating the connectivity rules (for network features)
  • Performing custom validation (for classes with class extensions implementing IObjectClassValidation)
  • Validating the relationship rules
For more information on how to work with rules, see Working with geodatabase rules.

Performing validation

Validation is a short-circuiting process, meaning that the validate process stops if one of the checks fails. For example, if a feature is found to violate a connectivity rule, the validation of the feature stops, and any relationship rules will not be checked until the connectivity rule violation is corrected and the feature is validated again.
Object classes and feature classes implement the IValidation interface, which defines the methods used for validation of the following multiple features—Validate, ValidateSelection, and ValidateSet. This interface also defines the Rules, RulesByField, and RulesBySubtypeCode properties to determine the type of rules that are being validated.
IValidate defines two methods—AddRule and DeleteRule—that are deprecated and should not be used.
The following code example shows how to perform validation on an entire feature class, writing the ObjectIDs of any invalid features to the console:
[C#]
public void DisplayInvalidFeatures(IFeatureClass featureClass)
{
    // Cast the feature class to the IValidation interface to validate.
    IValidation validation=(IValidation)featureClass;
    ISelectionSet selectionSet=validation.Validate(null, null);

    // Iterate through the selection set's IDs.
    IEnumIDs enumIDs=selectionSet.IDs;
    int invalidOID= - 1;
    while ((invalidOID=enumIDs.Next()) !=  - 1)
    {
        Console.WriteLine(invalidOID);
    }
}
[VB.NET]
Public Sub DisplayInvalidFeatures(ByVal featureClass As IFeatureClass)
    ' Cast the feature class to the IValidation interface to validate.
    Dim validation As IValidation=CType(featureClass, IValidation)
    Dim selectionSet As ISelectionSet=validation.Validate(Nothing, Nothing)
    
    ' Iterate through the selection set's IDs.
    Dim enumIDs As IEnumIDs=selectionSet.IDs
    Dim invalidOID As Integer=enumIDs.Next
    While invalidOID <> -1
        Console.WriteLine(invalidOID)
        invalidOID=enumIDs.Next
    End While
End Sub
To determine why an object is deemed invalid, the object must be validated individually. Objects and features implement the IValidate interface, which defines the Validate, GetInvalidFields, GetInvalidRules, and GetInvalidRulesByField methods. The Validate method has a Boolean return type indicating the object's validity, and an outbound string parameter that provides a human-readable explanation if the object is invalid. IValidate.Validate is similar to IValidation.Validate in that it validates in a short-circuiting manner. The following code example modifies the previous code example to provide explanations of invalidity:
[C#]
public void DisplayInvalidFeaturesWithReasons(IFeatureClass featureClass)
{
    // Cast the feature class to the IValidation interface to validate.
    IValidation validation=(IValidation)featureClass;
    ISelectionSet selectionSet=validation.Validate(null, null);

    // Iterate through the selection set's IDs.
    IEnumIDs enumIDs=selectionSet.IDs;
    int invalidOID= - 1;
    while ((invalidOID=enumIDs.Next()) !=  - 1)
    {
        IFeature feature=featureClass.GetFeature(invalidOID);
        IValidate validate=(IValidate)feature;
        String validateMessage=null;
        validate.Validate(out validateMessage);
        Console.WriteLine("{0}: {1}", invalidOID, validateMessage);
    }
}
[VB.NET]
Public Sub DisplayInvalidFeaturesWithReasons(ByVal featureClass As IFeatureClass)
    ' Cast the feature class to the IValidation interface to validate.
    Dim validation As IValidation=CType(featureClass, IValidation)
    Dim selectionSet As ISelectionSet=validation.Validate(Nothing, Nothing)
    
    ' Iterate through the selection set's IDs.
    Dim enumIDs As IEnumIDs=selectionSet.IDs
    Dim invalidOID As Integer=enumIDs.Next
    While invalidOID <> -1
        Dim feature As IFeature=featureClass.GetFeature(invalidOID)
        Dim validate As IValidate=CType(feature, IValidate)
        Dim validateMessage As String=Nothing
        validate.Validate(validateMessage)
        Console.WriteLine("{0}: {1}", invalidOID, validateMessage)
        invalidOID=enumIDs.Next
    End While
End Sub
Custom validation can be implemented on a class extension to enforce any business logic that cannot be satisfied using an out-of-the-box rule. Examples of this could include ensuring that a string field's value is a valid uniform resource locator (URL), defining rules that take the values of multiple fields into account, or applying constraints to an attributed relationship's field values. For more information on custom validation, see the IObjectClassValidation section in the topic, Creating class extensions.


See Also:

Working with geodatabase rules




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):

Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine