Michelle was hired by a software company to "clean up" an ASP.NET application they have been developing/maintaining for the last three or four years. A lot of the former team members were begrudgingly forced out of their VB6 comfort zones to use new-fangled things like "Objects", so you can imagine the state of the code. There is nothing unique about the quality (or lack-there-of) of this wonderful piece of code, but it was the accompanying comments that inspired Michelle to send it and me to post it ...
'Way To Go O.O. !!! 'Gotta love this .NET. Now I need to *KNOW* whether or not 'it's a Option or Checkbox. They both have a Checked property 'but you know, ctrl.Checked = True would just be toooo hard for '.NET wouldn't it?! For Each ctrl In Controls Select Case ctrl.ToString Case "System.Web.UI.LiteralControl" 'only looking for Check/Radios -- not these Case "System.Web.UI.ResourceBasedLiteralControl" 'same Case Else strCtrlId = Trim(ctrl.ID.ToString) 'Is the current control the one I want? If LCase(strCtrlId) = LCase(Trim(strCheckedControl)) Then 'It is a checkbox, so check it If InStr(strCtrlId, "chk") = 1 Then Dim chkTemp As CheckBox = CType(ctrl, CheckBox) chkTemp.Checked = True Exit For 'It is a radio, so ... *gasp* ... check it! ElseIf InStr(strCtrlId, "opt") = 1 Then Dim optTemp As RadioButton = CType(ctrl, RadioButton) optTemp.Checked = True Exit For End If End If End Select Next
Michelle has since replaced the block with the desired OO principle ...
Dim thisControl As Control = FindControl(controlName) If Not thisControl Is Nothing AndAlso TypeOf (thisControl) Is ICheckBoxControl Then CType(thisControl, System.Web.UI.ICheckBoxControl).Checked = True End If
... but left the legacy version there and commented out for future programmers to enjoy.