ArcObjects Library Reference  (GeoDatabase)    

IObjectClassValidation Example

[Visual Basic 6.0]

The following code demonstrates simple custom validation that requires the value of the field to be divisible by two.

To use this example:

  1. Paste this code into a class module in a ActiveX dll Visual Basic project.
  2. Compile the project to create the dll.
  3. Use the IClassSchemaEdit interface to put the GUID of this class extension in the EXTCLSID property for the object class or feature class that you want to associate this class extension with.
  4. To test this class extension, add the feature class or object class to ArcMap, select some objects and use the Validate Selection command and if the rule is violated, those objects will be returned as invalid.
Implements IObjectClassExtension
Implements IObjectClassValidation
Private m_pClassHelper As IClassHelper
Private m_myValueIndex As Long
Private Const MYVALUE_FIELDNAME As String = "MyValue"
Private Sub IClassExtension_Init(ByVal pClassHelper As esriGeoDatabase.IClassHelper, ByVal pExtensionProperties As esriSystem.IPropertySet)
  Set m_pClassHelper = pClassHelper
  Dim pClass As IClass
  Set pClass = m_pClassHelper.Class
  m_myValueIndex = pClass.FindField(MYVALUE_FIELDNAME)
End Sub
Private Sub IClassExtension_Shutdown()
  ‘Release Helper here
  Set m_pClassHelper = Nothing
End Sub
Private Function IObjectClassValidation_ValidateField(ByVal Row As esriGeoDatabase.IRow, ByVal FIELDNAME As String) As String
  Dim errStr As String
  errStr = ""
  If FIELDNAME = MYVALUE_FIELDNAME Then
    Dim myValue As Long
    myValue = Row.value(m_myValueIndex)
    Dim modValue As Long
    modValue = myValue Mod 2
    If modValue > 0 Then
      errStr = "Field value is not divisible by 2."
    End If
  End If
  IObjectClassValidation_ValidateField = errStr
End Function
Private Function IObjectClassValidation_ValidateRow(ByVal Row As esriGeoDatabase.IRow) As String
  IObjectClassValidation_ValidateRow = _
  IObjectClassValidation_ValidateField(Row, MYVALUE_FIELDNAME)
End Function

[Visual Basic .NET, C#, C++]
No example is available for Visual Basic .NET, C#, or C++. To view a Visual Basic 6.0 example, click the Language Filter button Language Filter in the upper-left corner of the page.