Use sparingly!
by
jeff_d_programmer
·
about 18 years, 1 month ago
In reply to Save time with control arrays
While control arrays can be useful for keeping blocks or code together, they have two major drawbacks:
1. They break the cardinal rule of procedures and functions – a process should only encompass one piece of functionality. If it must do more, break it into discreet pieces and separate them out into separate functions. By breaking this rule you make your code more difficult to read, less reusable, and more difficult to modify (a change to a block of code is much more likely to have unintended consequences).
2. They do not upgrade to VB.Net, nor are they available in most other languages, thus making them impossible to port or upgrade.
Microsoft VB Wizards creates them in only one instance – the buttons on a toolbar – and this is only because an individual button on a toolbar does not raise its own event. Even then you should call out to a separate procedure for the actual functionality of the action. Example:
Private Sub tbToolBar_ButtonClick(ByVal Button As MSComCtlLib.Button)
On Error Resume Next
Select Case Button.Key
Case “New”
Call Create_New()
Case “Open”
Call mnuFileOpen_Click
Case “Save”
Call mnuFileSave_Click
Case “Print”
Call mnuFilePrint_Click
Case “Cut”
Call mnuEditCut_Click
Case “Copy”
Call mnuEditCopy_Click
Case “Paste”
Call mnuEditPaste_Click
End Select
End Sub
Thus you can see that using control arrays is actually a bastardization of the select case statement. If you feel you MUST use it, use it sparingly and give plenty of fore-thought as to WHY your overriding the most basic of programming principles.