Functions: Shadowing

Over the years, functions has grown to become a quite complex and flexible feature in Visual Basic. The striking flexibility of the parameters, the modifiers that can apply a function and the function attributes, gives you great possibilities when you are coding. Some of the techniques include multiple return values and recursion and some of the features include passing by value, passing by reference, parameter arrays and shadowing. Shadowing is a feature that I find very useful when designing dialogs.

The ShowDialog function, that is inherited from the Form class, is a common subject of shadowing for me. It holds two overloads, one parameterless and one that takes the owner form. Shadowing lets you create a new version of a function, no matter if it is overridable or not, and at the same time block all current overloads. So any form that I want to add a mandatory parameter to, I shadow the ShowDialog function. This is a typical function declaration in my Windows applications:

Public Shadows Function ShowDialog(ByVal Owner As IWin32Window, _
ByVal CustomerID As Integer) As DialogResult

By automagic, the current versions of ShowDialog in my form disappears, and this function (the function that does the shadowing) is the one that can be called to display the form as a dialog. A implementation could look like this:

Public Shadows Function ShowDialog(ByVal Owner As IWin32Window, _
ByVal CustomerID As Integer) As DialogResult
   Me.mCustomerID = CustomerID
   Return MyBase.ShowDialog(Owner)
End Function

The initialization would then be done in the Load event.

Shadowing is one of the killer features that from what I know is quite unique for Visual Basic.

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll:

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Comments

Important information: If you have not commented before, your comment will be reviewed before it is published. This means that you will not see it immediately, but I have received it. This is not because I want to filter comments, but because I want to prevent spam and advertising.

Leave a Reply

Your email address will not be published. Required fields are marked *