Re: reduce redundant code




"MP" <NoSpam@xxxxxxxxxx> schrieb im Newsbeitrag
news:Oz$SIG8gIHA.536@xxxxxxxxxxxxxxxxxxxxxxx

ok ordered it, thanks for the suggestion
so am I reading too much between the lines to assume
you're basically saying my sample is totally f*ed up?
About how many different filtered object-types
do we talk here?
20-30?
100-200?

Assuming you have <=32 Obj-Types, you could solve
the first part of your task (the ObjectFiltering) with low
efforts using Enums - just look at the example below.

The second part (the Propertycheck) seems very special,
but you could also solve that in a generic and procedural
way, as long as your Prop-Checks don't get too complex.
But in this case you will have to write special Compare-
Implementations in either way, be it an OO-approach
or a procedural one.

But look at the Code first, maybe the usage of Enums
demonstrated there gives you at least a few ideas, how
to reduce the ParamCount generally in such scenarios.

'***Into a Form (place at least a TextBox and a Label on it)
Option Explicit

Private Enum oFlags
Label = 1
CommandButton = 2
TextBox = 4
ListBox = 8
ComboBox = 16
'... etc
End Enum

Private Enum ObjFilters
ObjectsWithCaption = oFlags.Label Or oFlags.CommandButton
ObjectsWithText = oFlags.TextBox Or oFlags.ListBox Or oFlags.ComboBox
End Enum

Private Enum PropertiesToCheck
Tag_Caption 'check for Tag- and for Caption-Props
Tag_Text 'check for Tag- and for Text-Props
'etc...
End Enum

Private Sub Form_Click()
Dim CurObjFilter As ObjFilters, Obj

CurObjFilter = ObjectsWithCaption 'use intellisense on your Filter
For Each Obj In Me.Controls
If CurObjFilter And oFlag(Obj) Then
Print Obj.Name, CheckPropertiesOn(Obj, Tag_Caption, "", "Label1")
End If
Next Obj

CurObjFilter = ObjectsWithText 'use intellisense on your Filter
For Each Obj In Me.Controls
If CurObjFilter And oFlag(Obj) Then
Print Obj.Name, CheckPropertiesOn(Obj, Tag_Text, "", "Text1")
End If
Next Obj
End Sub

'Helper, to map a BitFlag to each Obj-Type using our oFlags.Enum
Private Function oFlag(Obj) As oFlags 'sort this list by "chance of
occurence"
If TypeOf Obj Is Label Then oFlag = oFlags.Label: Exit Function
If TypeOf Obj Is CommandButton Then oFlag = oFlags.CommandButton: Exit
Function
If TypeOf Obj Is TextBox Then oFlag = oFlags.TextBox: Exit Function
If TypeOf Obj Is ListBox Then oFlag = oFlags.ListBox: Exit Function
If TypeOf Obj Is ComboBox Then oFlag = oFlags.ComboBox: Exit Function
End Function

'returns false if not all properties match
Private Function CheckPropertiesOn(Obj, Props As PropertiesToCheck, _
ParamArray P()) As Boolean
Dim i&, PropNames() As String
If UBound(P) = -1 Then Exit Function

PropNames = GetPropNames(Props)

If UBound(PropNames) <> UBound(P) Then
Exit Function 'ParamCount doesn't match the Properties Count
End If

For i = 0 To UBound(PropNames)
If CallByName(Obj, PropNames(i), VbGet Or VbMethod) <> P(i) Then
Exit Function
End If
Next i

CheckPropertiesOn = True 'in case all properties match
End Function

'Helper, to map to an Array from the Prop-Combi-Enum 'PropertiesToCheck'
Private Function GetPropNames(Props As PropertiesToCheck) As String()
Select Case Props
Case Tag_Caption: GetPropNames = Split("Tag,Caption", ",")
Case Tag_Text: GetPropNames = Split("Tag,Text", ",")
'etc...
Case Else: GetPropNames = Split("") 'Return with an ubound of -1
End Select
End Function


Olaf


.