RELAXED DELEGATES
If you assign a variable to the value in a variable of a different type, Visual Basic automatically converts the value into the correct type under some circumstances. If you set a Single variable equal to an Integer variable, Visual Basic automatically converts the Integer into a Single.
If Option Strict is off, you can also do the reverse: If you assign an Integer variable equal to a Single variable, Visual Basic converts the Single into an Integer (if it can).
In a similar manner, relaxed delegates let Visual Basic convert method parameters from one data type to another under certain circumstances. If the code invokes a subroutine by using a delegate, Visual Basic tries to convert parameters when it can. Probably the easiest way to understand how this works is to consider an example.
The following code declares a delegate type named TestDelegate. Methods that match this delegate should be subroutines that take a Control as a parameter.
' Declare the delegate type.
Private Delegate Sub TestDelegate(ctl As Control)
The following code defines three subroutines that take parameters of different types. The first takes an Object as a parameter, the second takes a TextBox, and the third takes no parameters. Note that the first subroutine cannot work if Option Strict is on. Option Strict disallows late binding, so the code cannot use a Text property provided by a generic Object.
' A more general parameter type. Private Sub Test1(obj As Object) obj.Text = "Test1" ' Needs ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access