Removing from a collection

One issue I had when I first started was how do I remove a child object from a collection. For example if I want to remove one constituent code from a constituent I need to loop through them, find the one I want to remove and remove it. My fear though was that if I simply remove it whilst looping won’t I mess up the collection? This can happen in some classes.

Apparently not with RE collections. You are able to loop and remove and not affect the collection. Here is the simple code to remove a constituent code:

Public Sub RemoveConCode(oRecord As IBBDataObject)
   Dim oConstit As CRecord
   Dim oConCode As CConstituentCode

   If TypeOf oRecord Is CRecord Then
      Set oConstit = oRecord

      For Each oConCode In oConstit.ConstituentCodes
         If oConCode.Fields(CONSTITUENT_CODE_fld_LONG_DESC_READONLY) = "To delete" Then
            oConstit.ConstituentCodes.Remove oConCode
         End If
   End If

End Sub