Removing Objects from Collections
This section shows example code to remove objects from collections. The example code uses:
//////////////////////////////////////////////////////////////////////
// RemDestColl function
//
// Example of using the CMetaObject::RemoveFromDestCollection function
///////////////////////////////////////////////////////////////////////
HRESULT RemDestColl(CMetaRepository& repos)
{
HRESULT result = S_OK;
CMetaObject obj;
OBJECTID_t
RelIDDivMakesAutos = NULLLOID,
loidEuroDiv = NULLLOID,
loidTornado = NULLLOID;
// Get ObjectIDs of EuroDiv, Tornado and the ManufofAutos Relationship
result = repos.GetObjectID(OBJGUID_EuroDivision, &loidEuroDiv);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_Tornado, &loidTornado);
if (SUCCEEDED(result))
result = repos.GetObjectID(RELGUID_DivMakesAutos,
&RelIDDivMakesAutos);
if (SUCCEEDED(result))
{
// Set local object ObjectID to EuroDivision
obj.SetObjectID(loidEuroDiv);
// Call RemoveFromDestCollection to remove Tornado from
// the collection of EuroDivision
result = obj.RemoveFromDestCollection(loidTornado, NULLGUID,
RelIDDivMakesAutos);
}
return (result);
} // RemDestColl
///////////////////////////////////////////////////////////////////////
// RemOrigColl function
//
// Example of using the CMetaObject::RemoveFromOrigCollection function
///////////////////////////////////////////////////////////////////////
HRESULT RemOrigColl(CMetaRepository& repos)
{
HRESULT result = S_OK;
CMetaObject obj;
OBJECTID_t
loidAA = NULLLOID,
loidYearling = NULLLOID;
// Get ObjectIDs of AmericanAutos and Yearling
result = repos.GetObjectID(OBJGUID_AmericanAutos, &loidAA);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_Yearling, &loidYearling);
if (SUCCEEDED(result))
{
// Set local object ObjectID to Yearling
obj.SetObjectID(loidYearling);
// Call RemoveFromOrigCollection to remove AmericanAutos from
// the collection of Yearling
result = obj.RemoveFromOrigCollection(loidAA,
RELGUID_DivMakesAutos);
}
return (result);
} // RemOrigColl
//////////////////////////////////////////////////////////////////////////
// RemMnyDestColl function
//
// Example of using the CMetaObject::RemoveManyFromDestCollection function
//////////////////////////////////////////////////////////////////////////
HRESULT RemMnyDestColl(CMetaRepository& repos)
{
LLOBJECTID_t inLoids; // list of loids to remove from a collection
HRESULT result = S_OK;
CMetaObject obj;
OBJECTID_t
RelIDDivMakesAutos = NULLLOID,
loidEuroDiv = NULLLOID,
loidTornado = NULLLOID,
loidHurricane = NULLLOID;
// Get the Object IDs of EuroDivison, Tornado, Hurricane and the ManufofAutos
// relationship
result = repos.GetObjectID(OBJGUID_EuroDivision, &loidEuroDiv);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_Tornado, &loidTornado);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_Hurricane, &loidHurricane);
if (SUCCEEDED(result))
result = repos.GetObjectID(RELGUID_DivMakesAutos,
&RelIDDivMakesAutos);
if (SUCCEEDED(result))
{
// put ObjectIDs of Tornado and Hurricane on a list
inLoids.push_back(loidTornado);
inLoids.push_back(loidHurricane);
// set the local object ObjectID to EuroDivision
obj.SetObjectID(loidEuroDiv);
// Call RemoveManyFromDestCollection to remove Tornado and
// Hurricane from the collection of EuroDivision
result = obj.RemoveManyFromDestCollection(inLoids, NULLGUID,
RelIDDivMakesAutos);
}
CLEARVECTOR(inLoids);
return (result);
} // RemMnyDestColl
//////////////////////////////////////////////////////////////////////////
// RemMnyOrigColl function
//
// Example of using the CMetaObject::RemoveManyFromOrigCollection function
//////////////////////////////////////////////////////////////////////////
HRESULT RemMnyOrigColl(CMetaRepository& repos)
{
LLOBJECTID_t inLoids; // list of loids to remove from a collection
HRESULT result = S_OK;
CMetaObject obj;
OBJECTID_t
loidAA = NULLLOID,
loidYearling = NULLLOID;
// Get the Object IDs of AmericanAutos and Yearling
result = repos.GetObjectID(OBJGUID_AmericanAutos, &loidAA);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_Yearling, &loidYearling);
if (SUCCEEDED(result))
{
// Put the ObjectID of AmericanAutos on a list
inLoids.push_back(loidAA);
// Set the local object ObjectID to Yearling
obj.SetObjectID(loidYearling);
// Call RemoveManyFromOrigCollection to remove AmericanAutos
// from the collection of Yearling
result = obj.RemoveManyFromOrigCollection(inLoids,
RELGUID_DivMakesAutos);
}
CLEARVECTOR(inLoids);
return (result);
} // RemMnyOrigColl