Developer Tips - Using the Field Checker
This post was written by geodatabase Product Engineer James MacKay. James works on the geodatabase development team and is responsible for a lot of the geodatabase SDK that is generated.
The IFieldChecker interface provides a way to validate fields for a specific workspace before they’re created. Reserved keywords, special characters and maximum field name lengths are properties that vary between different types of workspaces and DBMSs; the field checker will not only detect fields that violate these rules, but it will generate a “fixed” fields collection with a similar (but valid) name. There are three common cases where field checkers are useful:
A field checker should always be used whenever an application allows users to manually enter field names, but it’s a good defensive programming pattern to use them all of the time. The following code shows how to validate fields prior to a CreateTable or CreateFeatureClass call.
Geoprocessing users: The geoprocessor also exposes field checking capability. See this article for more information.
public IFields ValidateFields(IWorkspace workspace, IFields fields)
{
// Create and initialize a field checker.
IFieldChecker fieldChecker = new FieldCheckerClass();
fieldChecker.ValidateWorkspace = workspace;
// Generate a validated fields collection.
IFields validatedFields = null;
IEnumFieldError enumFieldError = null;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
// You can either notify the user of any errors or just skip this step.
IFieldError fieldError = null;
while ((fieldError = enumFieldError.Next()) != null)
{
Console.WriteLine("Error in field {0}: {1}", fieldError.FieldIndex, fieldError.FieldError);
}
// Return the validated fields.
return validatedFields;
}