' Visual Basic 2008 9.0 .NET Examples - Relaxed Delegates - New in Visual Basic 2008 - 9.0 : 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 ' (1) Console.WriteLine(delegate1a.Invoke(1)) ' Dim delegate1b As Delegate1 = AddressOf Method2 ' (2) Console.WriteLine(delegate1b.Invoke(1)) ' Dim delegate1c As Delegate1 = AddressOf Method3 ' (3) Console.WriteLine(delegate1c.Invoke(1)) ' Dim delegate1d As Delegate1 = AddressOf Method4 ' (4) Console.WriteLine(delegate1d.Invoke(1)) ' Dim delegate1e As Delegate1 = AddressOf Method5 ' (5) Console.WriteLine(delegate1e.Invoke(1)) ' Dim delegate1f As Delegate1 = AddressOf Method6 ' (6) Console.WriteLine(delegate1f.Invoke(1)) ' Dim delegate1g As Delegate1 = AddressOf Method7 ' (7) Console.WriteLine(delegate1g.Invoke(1)) ' Dim delegate2a As Delegate2 = AddressOf Method1 ' (8) 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 ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.