Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Recursive methods. [modified]

Recursive methods. [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
dockerhelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Sander RosselS Offline
    Sander RosselS Offline
    Sander Rossel
    wrote on last edited by
    #1

    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 ctl

    End 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 here

    If canSave(Me) Then
    SaveNew()
    End If

    ' Some more code here
    End Sub

    Private Function canSave(ByVal obj as Control) As Boolean
    Dim bool as Boolean = True

    For 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 ctl

    Return 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

    _ realJSOPR 2 Replies Last reply
    0
    • Sander RosselS Sander Rossel

      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 ctl

      End 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 here

      If canSave(Me) Then
      SaveNew()
      End If

      ' Some more code here
      End Sub

      Private Function canSave(ByVal obj as Control) As Boolean
      Dim bool as Boolean = True

      For 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 ctl

      Return 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

      _ Offline
      _ Offline
      _Erik_
      wrote on last edited by
      #2

      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.

      Sander RosselS 1 Reply Last reply
      0
      • _ _Erik_

        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.

        Sander RosselS Offline
        Sander RosselS Offline
        Sander Rossel
        wrote on last edited by
        #3

        Well... Actually I (the new developer) has to pay :( :P

        R 1 Reply Last reply
        0
        • Sander RosselS Sander Rossel

          Well... Actually I (the new developer) has to pay :( :P

          R Offline
          R Offline
          RobCroll
          wrote on last edited by
          #4

          Hopefully the programmer was so called "Senior". That's a tough call for a junior programmer.

          1 Reply Last reply
          0
          • Sander RosselS Sander Rossel

            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 ctl

            End 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 here

            If canSave(Me) Then
            SaveNew()
            End If

            ' Some more code here
            End Sub

            Private Function canSave(ByVal obj as Control) As Boolean
            Dim bool as Boolean = True

            For 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 ctl

            Return 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

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups