StrataFrame Forum

Looping through BO fields

http://forum.strataframe.net/Topic7244.aspx

By Ben Kim - 3/1/2007

I want to create a generic sub that will loop through the BO field collection, picking up only the database fields of course, determine the field type and if it is of type String, trimend.

Right now I have code like this in the CheckRulesOnCurrentRow event:

' Trim off trailing spaces
Me.ClassType = Me.ClassType.TrimEnd()
Me.LocalState = Me.LocalState.TrimEnd()
Me.OffenseClass = Me.OffenseClass.TrimEnd()
Me.DescriptionText = Me.DescriptionText.TrimEnd()

and so forth for every string based field.  I would like to do something like (pseudo code):

Public Sub TrimAll(ByRef MyBO As BusinessObject)

    For Each DBField In MyBO ' How to determine the property is a DBField???
          If DBField.Type = StringType  ' Is it stored in the BO as a String or as the DBType?
               DBField.FieldValue = Me.DBField.TrimEnd()
          End If

    End For
End Sub

Again this way I can create a generic sub, pass the BO to the function from the CheckRulesOncurrentRow event and have the fields trimmed for me instead of listing dozens sometimes hundreds (depends on table) of fields to do the same.

Sorry for all of the newbie type questions!

Ideas?

Ben

By StrataFrame Team - 3/2/2007

You could cycle through the fields in the AllFields collection like this:

For Each field As String In BO.AllFields
    Select Case BO.FieldDbTypes(field)
        Case System.Data.DbType.String, System.Data.DbType.StringFixedLength, _
            System.Data.DbType.AnsiString, System.Data.DbType.AnsiStringFixedLength
            '-- Trim the field
            BO.Item(field) = CType(BO.Item(field), String).Trim()
        Case Else
            '-- Do whatever else you want to do to the other data types
Next

By StrataFrame Team - 3/2/2007

Oh, and you won't have to pass the business object by reference... ByVal will work fine since you aren't going to return a different business object to the calling method... business objects are classes, which are already reference types.