Recursive methods. [modified]
-
I love (unending) recursive methods... I recently had one which took me some time to fix too. I needed to get a list with all the controls in a form. Some code calls this function with the current form as obj and an empty list of controls as list.
Private Sub GetControls(Byval obj as Control, ByRef list as List (Of Control))
For each ctl as Control in obj.Controls
If ctl.Controls.Count > 0
GetControls(obj, list)
list.Add(ctl)
Else
list.Add(ctl)
End If
Next ctlEnd Sub
I just didn't see I kept passing on the same form over and over again :laugh: Even better is a recursive method I recently found in production code. Our company uses their own controls which have a Mandatory property. So before saving data we want to check if all controls that have Mandatory set to true have a value. I don't remember the actual names of the methods, but it doesn't really matter anyway ;)
Public Sub Save()
' Some code hereIf canSave(Me) Then
SaveNew()
End If' Some more code here
End SubPrivate Function canSave(ByVal obj as Control) As Boolean
Dim bool as Boolean = TrueFor each ctl as Control in obj.Controls
If ctl.Controls.Count > 0
canSave(ctl)
If TypeOf ctl is CompanyControl Then
Dim cctl As CompanyControl = ctl
If cctl.Mandatory AndAlso cctl.Value = "" Then
bool = False
End If
End If
Else
If TypeOf ctl is CompanyControl Then
Dim cctl as CompanyControl = ctl
If cctl.Mandatory AndAlso cctl.Value = "" Then
bool = False
End If
End If
End If
Next ctlReturn bool
End Function
The form it was used on had only 3 controls outside a container. So I added a mandatory field on a tab, kept it empty and... See how that would return true in most cases? :)
modified on Wednesday, December 22, 2010 3:35 PM
-
I love (unending) recursive methods... I recently had one which took me some time to fix too. I needed to get a list with all the controls in a form. Some code calls this function with the current form as obj and an empty list of controls as list.
Private Sub GetControls(Byval obj as Control, ByRef list as List (Of Control))
For each ctl as Control in obj.Controls
If ctl.Controls.Count > 0
GetControls(obj, list)
list.Add(ctl)
Else
list.Add(ctl)
End If
Next ctlEnd Sub
I just didn't see I kept passing on the same form over and over again :laugh: Even better is a recursive method I recently found in production code. Our company uses their own controls which have a Mandatory property. So before saving data we want to check if all controls that have Mandatory set to true have a value. I don't remember the actual names of the methods, but it doesn't really matter anyway ;)
Public Sub Save()
' Some code hereIf canSave(Me) Then
SaveNew()
End If' Some more code here
End SubPrivate Function canSave(ByVal obj as Control) As Boolean
Dim bool as Boolean = TrueFor each ctl as Control in obj.Controls
If ctl.Controls.Count > 0
canSave(ctl)
If TypeOf ctl is CompanyControl Then
Dim cctl As CompanyControl = ctl
If cctl.Mandatory AndAlso cctl.Value = "" Then
bool = False
End If
End If
Else
If TypeOf ctl is CompanyControl Then
Dim cctl as CompanyControl = ctl
If cctl.Mandatory AndAlso cctl.Value = "" Then
bool = False
End If
End If
End If
Next ctlReturn bool
End Function
The form it was used on had only 3 controls outside a container. So I added a mandatory field on a tab, kept it empty and... See how that would return true in most cases? :)
modified on Wednesday, December 22, 2010 3:35 PM
-
The horror here is not only that these methods do not do what they are supposed to do. The fact is that the implementation itself is really poor. Seems that the programmer got paid according to the number of lines of code he wrote.
Well... Actually I (the new developer) has to pay :( :P
-
Well... Actually I (the new developer) has to pay :( :P
-
I love (unending) recursive methods... I recently had one which took me some time to fix too. I needed to get a list with all the controls in a form. Some code calls this function with the current form as obj and an empty list of controls as list.
Private Sub GetControls(Byval obj as Control, ByRef list as List (Of Control))
For each ctl as Control in obj.Controls
If ctl.Controls.Count > 0
GetControls(obj, list)
list.Add(ctl)
Else
list.Add(ctl)
End If
Next ctlEnd Sub
I just didn't see I kept passing on the same form over and over again :laugh: Even better is a recursive method I recently found in production code. Our company uses their own controls which have a Mandatory property. So before saving data we want to check if all controls that have Mandatory set to true have a value. I don't remember the actual names of the methods, but it doesn't really matter anyway ;)
Public Sub Save()
' Some code hereIf canSave(Me) Then
SaveNew()
End If' Some more code here
End SubPrivate Function canSave(ByVal obj as Control) As Boolean
Dim bool as Boolean = TrueFor each ctl as Control in obj.Controls
If ctl.Controls.Count > 0
canSave(ctl)
If TypeOf ctl is CompanyControl Then
Dim cctl As CompanyControl = ctl
If cctl.Mandatory AndAlso cctl.Value = "" Then
bool = False
End If
End If
Else
If TypeOf ctl is CompanyControl Then
Dim cctl as CompanyControl = ctl
If cctl.Mandatory AndAlso cctl.Value = "" Then
bool = False
End If
End If
End If
Next ctlReturn bool
End Function
The form it was used on had only 3 controls outside a container. So I added a mandatory field on a tab, kept it empty and... See how that would return true in most cases? :)
modified on Wednesday, December 22, 2010 3:35 PM
The root of the problem is that it's done in VB...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997