|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Visual Basic 9.0 ( 2008 ) allowes delegate-instances to point to methods with a slightly different signature, then the signature defined by the delegatetype of that instance. |
| Option Strict Off
Class Counter
Private m_Value As Short
Public Sub New(ByVal value As Short)
m_Value = value
End Sub
Public ReadOnly Property Value() As Short
Get
Value = m_Value
End Get
End Property
Public Sub Raise()
m_Value = System.Convert.ToInt16(m_Value + 1)
End Sub
Public Shared Widening Operator CType(ByVal counter As Counter) As Integer
Return counter.Value
End Operator
Public Shared Narrowing Operator CType(ByVal value As Integer) As Counter
Return New Counter(System.Convert.ToInt16(value))
End Operator
End Class
Class Example1
Public Delegate Function Delegate1(ByVal argument1 As Integer) As Integer
Public Delegate Sub Delegate2(ByVal argument1 As Integer)
Public Shared Sub Main()
Dim delegate1a As Delegate1 = AddressOf Method1
Console.WriteLine(delegate1a.Invoke(1))
Dim delegate1b As Delegate1 = AddressOf Method2
Console.WriteLine(delegate1b.Invoke(1))
Dim delegate1c As Delegate1 = AddressOf Method3
Console.WriteLine(delegate1c.Invoke(1))
Dim delegate1d As Delegate1 = AddressOf Method4
Console.WriteLine(delegate1d.Invoke(1))
Dim delegate1e As Delegate1 = AddressOf Method5
Console.WriteLine(delegate1e.Invoke(1))
Dim delegate1f As Delegate1 = AddressOf Method6
Console.WriteLine(delegate1f.Invoke(1))
Dim delegate1g As Delegate1 = AddressOf Method7
Console.WriteLine(delegate1g.Invoke(1))
Dim delegate2a As Delegate2 = AddressOf Method1
delegate2a.Invoke(1)
Console.ReadLine()
End Sub
Public Shared Function Method1() As Integer
Console.WriteLine("Method1")
Method1 = 1
End Function
Public Shared Function Method2(ByVal argument1 As Long) As Integer
Method2 = 2
End Function
Public Shared Function Method3(ByVal argument1 As Short) As Integer
Method3 = 3
End Function
Public Shared Function Method4(ByVal argument1 As Integer) As Short
Method4 = 4
End Function
Public Shared Function Method5(ByVal argument1 As Integer) As Long
Method5 = 5
End Function
Public Shared Function Method6(ByVal argument1 As Counter) As Integer
Method6 = 6
End Function
Public Shared Function Method7(ByVal argument1 As Integer) As Counter
Method7 = New Counter(7)
End Function
End Class Download Broncode |
| Output : Method1
1
2
3
4
5
6
7
Method1 |
Relaxing by Converson between Datatypes
| Different datatypes for the argumeent or return values are allowed as long as conversion between those types is possible. Don't forget that 'Option Strict On' will disallow narrowing conversions.
With Option Strict On ( and Off ) (2), (4) and (7) are allowed :
(1) delegate1b of type Delegate1 ( that normally expects an Integer agument ) can point to a function with an argument of a type that is convertable ( through widening conversion ) to Integer. When delegate1b is invoked an Integer argumentvalue is expected. Integer to Long is widening conversion.
(4)(7) delegate1d en delegate1g of type Delegate1 ( that normally returns an Integer value ) can point to a function with a returntype to which Integer can convert to ( through widening conversion ). When delegate1d or delegate1g is invoked an Integer value is returned. An Integer expression can represent values of types Short and Counter.
Option Strict Off allows (3), (5) and (6) :
(3)(6) 'delegate1c' and 'delegate1f' of type 'Delegate1' ( that normally expects an 'Integer' argument' ) can point to a function with an argument of a type that 'Integer' can convert to. When 'delegate1c' or 'delegate1f' is invoked an 'Integer' argumentvalue is expected. As long as the 'Integer' argumentvalue is representable as a 'Short' (3) or a 'Counter' (6) and permissive type semantics ( 'Option Strict Off' ) is used, those invocations are possible.
(5) delegate1e of type Delegate1 ( that normally returns an Integer value ) can point to a function with a returntype convertable to Integer. When delegate1e is invoked an Integer value is returned. As long as the methods returns a value ( here Long ) representable as an Integer, no problem occurs when those methodes are invoked. |
Up
Relaxing by Omniting Parameter Specifications
| Option Strict Off allows delegate-instances to point to methods where the parameter specifications of the delegatetype are omnited. The definition, of the member being pointed at, has to define all parameters or has to omnit all parameter specifications.
Option Strict Off allows :
(1) delegate1a of type Delegate1 ( that normally expects an Integer argument ) can point to a function that has not defined any parameters. When delegate1a is invoked a Integer value is expected, but not used. |
Up
Relaxing by Omniting Return Values
| Option Strict Off allows delegate-instances to point to functions ( Functions ) when instances of that delegatetype would normally point to procedures ( Subs ).
Option Strict Off allows (8) :
(8) delegate2a of type Delegate2 ( that normally point to a Sub ) can point to a Function. Invoking delegate2a is still a call to a procedure ( Sub ), the returnvalue of Method1 will not be used. |
Up
Eventhandlers
| Especially the relaxation by omniting parameter specifications is very usefull when defining eventhandlers. Often the eventhandler will not use the argumentvalues provided by the event, in that case the eventhandler can simply omnit these specifications.
The signatures in next example of eventhandlers Form.Button1_Click and Example2.Button1_Click are identical to the signature of the delegatetype created by the defined event. |
| Class Button
Public Event Click As EventHandler(Of EventArgs)
Protected Sub OnClick()
RaiseEvent Click(Me, EventArgs.Empty)
End Sub
Public Sub SimulateClick()
OnClick()
End Sub
End Class
Class Form
Friend WithEvents Button1 As New Button
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
Console.WriteLine("Form.Button1_Click")
End Sub
End Class
Class Example2
Public Shared Sub Main()
Dim form1 As New Form
AddHandler form1.Button1.Click, AddressOf Button1_Click
form1.Button1.SimulateClick()
Console.ReadLine()
End Sub
Private Shared Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs)
Console.WriteLine("Example2.Button1_Click")
End Sub
End Class Download Broncode |
| Output : Form.Button1_Click
Example2.Button1_Click |
| The above example did not use the relaxation capabilities for the eventhandlers, but next example will omnit the parameter specifications of ClickDelegate. |
| Partial Class Form
Friend WithEvents Button2 As New Button
Private Sub Button2_Click() Handles Button2.Click
Console.WriteLine("Form.Button2_Click")
End Sub
End Class
Class Example3
Public Shared Sub Main()
Dim form1 As New Form
AddHandler form1.Button2.Click, AddressOf Button2_Click
form1.Button2.SimulateClick()
Console.ReadLine()
End Sub
Private Shared Sub Button2_Click()
Console.WriteLine("Example2.Button2_Click")
End Sub
End Class Download Broncode |
| Output : Form.Button2_Click
Example2.Button2_Click |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|