The following code example creates a MemoryRelationshipClass between the us_states featureclass and the us_counties featureclass. It then uses the MemoryRelationshipClass to print the counties that appear in the state of California.
[Visual Basic 6.0]
Private Sub MemRelClassExample(pUS_StatesCl As IObjectClass, pUS_CountyCl As IObjectClass)
' ++ Create a MemoryRelationshipClass between states and counties
Dim pRelationshipClass As IRelationshipClass
Dim pMemRelationshipClassFact As IMemoryRelationshipClassFactory
Set pMemRelationshipClassFact = New MemoryRelationshipClassFactory
Set pRelationshipClass = pMemRelationshipClassFact.Open("test", pUS_StatesCl, _
"state_fips", pUS_CountyCl, "state_fips", "forward", "backward", esriRelCardinalityOneToMany)
' ++ Apply a filter to limit the return to just counties in California
Dim pQFilter As IQueryFilter
Set pQFilter = New QueryFilter
pQFilter.WhereClause = """STATE_NAME"" = 'California'"
Dim pFeatureClass As IFeatureClass
Dim pFCursor As IFeatureCursor
Dim pFeature As IFeature
Set pFeatureClass = pUS_StatesCl
Set pFCursor = pFeatureClass.Search(pQFilter, True)
Set pFeature = pFCursor.NextFeature
' ++ List the counties that relate to California
Dim pRelateSet As ISet
Dim pRowBuff As IRowBuffer
Set pRelateSet = pRelationshipClass.GetObjectsRelatedToObject(pFeature)
Set pRowBuff = pRelateSet.Next
Do While Not pRowBuff Is Nothing
Debug.Print pRowBuff.Value(2)
Set pRowBuff = pRelateSet.Next
Loop
End Sub
[C#]//MemoryRelationshipClass Example
//The following function creates a MemoryRelationshipClass between the us_states
//featureclass and the us_counties featureclass. It then uses the MemoryRelationshipClass
//to print the counties that appear in the state of California.
public void MemoryRelationshipClass_Example(IObjectClass usStatesClass, IObjectClass usCountyClass)
{
//create a MemoryRelationshipClass between states and counties
IMemoryRelationshipClassFactory memoryRelClassFactory = new MemoryRelationshipClassFactoryClass();
IRelationshipClass relationshipClass = memoryRelClassFactory.Open("test", usStatesClass, "state_fips",
usCountyClass, "state_fips", "forward", "backward", esriRelCardinality.esriRelCardinalityOneToMany);//apply a filter to limit the return to just counties in California
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "STATE_NAME = 'California'";
//cast for IFeatureClass
IFeatureClass featureClass = (IFeatureClass)usStatesClass;
//open a cursor with the filter, recycling can be used because no updates will be made.
IFeatureCursor featureCursor = featureClass.Search(queryFilter, true);
IFeature feature = featureCursor.NextFeature();
//list the counties that relate to California
ESRI.ArcGIS.esriSystem.ISet relateSet = relationshipClass.GetObjectsRelatedToObject((IObject)feature);
IRowBuffer rowBuffer = (IRowBuffer)relateSet.Next();
int fieldIndex = rowBuffer.Fields.FindFieldByAliasName("County Name");
while (rowBuffer != null)
{
Console.WriteLine("The county {0} is in California", rowBuffer.get_Value(fieldIndex));
rowBuffer = (IRowBuffer)relateSet.Next();
}
}
[Visual Basic .NET, C++]
No example is available for Visual Basic .NET or C++. To view a Visual Basic 6.0 or C# example, click the Language Filter button
in the upper-left corner of the page.