Creating Subtypes for an Object Class

Created:1/20/1999
Description:

Subtypes are a way that you can group features or objects within a feature class or object class. By assigning different default values and validation rules for the subtypes you can maintain the flexibility of the feature class while increasing the customization of your features. To establish subtypes for an object class and the default values for a subtype's fields, you use the ISubtypes interface on the object class. In the sample code below, a feature class called 'Buildings' is assigned the following subtypes:



Building (subtype code 0)

Commercial (subtype code 1)

Residential(subtype code 2)



The subtype column will be SYMBOL. Once the subtypes are created, the TYPE_ field is assigned default values for each subtype.


How to use:
  1. If necessary, Run the 'Creating and Populating a Geodatabase' sample from the Database Tools section. This will build the Greeley geodatabase.
  2. In ArcCatalog, open the Properties dialog on the buildings feature class.
  3. If subtypes aleady exist, delete them by setting the subtype field to None, then closing the dialog with OK.
  4. Paste the code into your VBA Application and run it.
Sub Add_subtypes()
  ' open the workspace
  Dim pFact As IWorkspaceFactory
  Set pFact = New AccessWorkspaceFactory
    
  Dim pWorkspace As IWorkspace
  Set pWorkspace = pFact.OpenFromFile("C:\Arcgis\arcexe83\ArcObjects Developer Kit\Samples\Geodatabase\Database Tools\Creating and Populating a Geodatabase\greeley.mdb", 0)
    
  ' get the dataset
  Dim pFeatws As IFeatureWorkspace
  Set pFeatws = pWorkspace
    
  Dim pFeatds As IFeatureDataset
  Set pFeatds = pFeatws.OpenFeatureDataset("SNLandbase")
    
  Dim pFeatclscont As IFeatureClassContainer
  Set pFeatclscont = pFeatds

  ' open the feature class
  Dim pFeatcls As IFeatureClass
  Set pFeatcls = pFeatclscont.ClassByName("SNBuildings")

  ' QI for the ISubtypes interface
  Dim pSubType As ISubtypes
  Set pSubType = pFeatcls
  
  ' Assign SYMBOL as the subtype field if not already setup
  If pSubType.SubtypeFieldName = "SYMBOL" Then Exit Sub
  pSubType.SubtypeFieldName = "SYMBOL"

  ' Add subtypes
  pSubType.AddSubtype 0, "Building"
  pSubType.AddSubtype 1, "Commercial"
  pSubType.AddSubtype 2, "Residential"
 
  ' Assign the default subtype code
  pSubType.DefaultSubtypeCode = 0
  
  ' Set default values for some fields for each subtype
  pSubType.DefaultValue(0, "TYPE_") = "BLD"
  pSubType.DefaultValue(1, "TYPE_") = "COM"
  pSubType.DefaultValue(2, "TYPE_") = "RES"

  MsgBox "Done"
End Sub