Polymorphism where art thou

If you don't know what this means, don't worry, because it isn't available as a programming technique in VBA; if you do, I'm sorry, you can't use it. The HideWorksheet procedure presented in Listing 7.4 would be a wonderful candidate for using polymorphism if it could be used.

What is polymorphism? Polymorphism is the practice of creating multiple versions of a procedure that are named the same but differ in terms of the parameters passed and used by each version of the procedure.

The best way to understand this is via example. The HideWorksheet procedure in Listing 7.4 requires that the name of a worksheet be passed to it using the first parameter, a string named sName. What if you wanted to have another version of the procedure that took a Worksheet object as a parameter instead? You could implement the procedure as follows.

1 Hides the worksheet

Sub HideWorksheet(ws As Worksheet, bVeryHidden As Boolean) If bVeryHidden Then ws.Visible = xlSheetVeryHidden

Else ws.Visible = xlSheetHidden End If End Sub

If polymorphism was allowed in VBA, you could place this procedure and the procedure from Listing 7.4 in the same module and use either one as your needs dictated. Because polymorphism can't be used, you can't have two procedures with the same name in the same module. For that matter, you can't have two procedures within the same project unless they're in separate modules, and then only one of the procedures can be public.

Because you can't use polymorphism, your only option is to give the procedures different names. For example, you could name one HideWorksheet and the other HideWorksheetName.

0 0

Post a comment