Click here to Skip to main content
15,887,828 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Performance Genius Pin
yxhu23-Jan-11 13:14
yxhu23-Jan-11 13:14 
GeneralUseless function Pin
musefan23-Dec-10 3:39
musefan23-Dec-10 3:39 
GeneralRe: Useless function Pin
OriginalGriff23-Dec-10 5:46
mveOriginalGriff23-Dec-10 5:46 
GeneralRe: Useless function Pin
musefan27-Dec-10 6:25
musefan27-Dec-10 6:25 
GeneralRe: Useless function Pin
RobCroll24-Dec-10 4:29
RobCroll24-Dec-10 4:29 
GeneralRe: Useless function Pin
musefan27-Dec-10 6:27
musefan27-Dec-10 6:27 
GeneralRe: Useless function Pin
fjdiewornncalwe27-Dec-10 5:45
professionalfjdiewornncalwe27-Dec-10 5:45 
GeneralRecursive methods. [modified] Pin
Sander Rossel22-Dec-10 9:28
professionalSander Rossel22-Dec-10 9:28 
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 | :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 Wink | ;)

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? Smile | :)
modified on Wednesday, December 22, 2010 3:35 PM

GeneralRe: Recursive methods. Pin
_Erik_23-Dec-10 3:58
_Erik_23-Dec-10 3:58 
GeneralRe: Recursive methods. Pin
Sander Rossel23-Dec-10 5:15
professionalSander Rossel23-Dec-10 5:15 
GeneralRe: Recursive methods. Pin
RobCroll24-Dec-10 4:46
RobCroll24-Dec-10 4:46 
GeneralRe: Recursive methods. Pin
#realJSOP26-Dec-10 5:04
mve#realJSOP26-Dec-10 5:04 
GeneralWhat a fright!! Pin
_Erik_22-Dec-10 4:55
_Erik_22-Dec-10 4:55 
GeneralRe: What a fright!! PinPopular
fjdiewornncalwe22-Dec-10 5:15
professionalfjdiewornncalwe22-Dec-10 5:15 
GeneralRe: What a fright!! Pin
_Erik_22-Dec-10 5:24
_Erik_22-Dec-10 5:24 
GeneralRe: What a fright!! Pin
Dwayne J. Baldwin27-Dec-10 7:31
Dwayne J. Baldwin27-Dec-10 7:31 
GeneralHow I accidentally slowed gr1d's server to a crawl... [modified] Pin
dawmail33321-Dec-10 22:08
dawmail33321-Dec-10 22:08 
GeneralRe: How I accidentally slowed gr1d's server to a crawl... Pin
Dwayne J. Baldwin27-Dec-10 7:58
Dwayne J. Baldwin27-Dec-10 7:58 
GeneralRe: How I accidentally slowed gr1d's server to a crawl... Pin
dawmail33327-Dec-10 13:14
dawmail33327-Dec-10 13:14 
GeneralRe: How I accidentally slowed gr1d's server to a crawl... Pin
wizardzz27-Dec-10 11:21
wizardzz27-Dec-10 11:21 
GeneralTesting the obvious.... Pin
Rob Grainger16-Dec-10 5:02
Rob Grainger16-Dec-10 5:02 
GeneralRe: Testing the obvious.... Pin
_Erik_16-Dec-10 5:10
_Erik_16-Dec-10 5:10 
GeneralRe: Testing the obvious.... Pin
Mike Winiberg16-Dec-10 20:36
professionalMike Winiberg16-Dec-10 20:36 
GeneralRe: Testing the obvious.... Pin
Brady Kelly16-Dec-10 20:39
Brady Kelly16-Dec-10 20:39 
GeneralRe: Testing the obvious.... Pin
mbb0116-Dec-10 21:25
mbb0116-Dec-10 21:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.