单一代理:
Module Module1
Public Delegate Sub basicdeleg(ByVal st As String) Sub Main() Dim d1 As New deleg() Dim DG1, DG2 As basicdeleg DG1 = AddressOf d1.eventchar DG2 = AddressOf d1.eventmess DG1("this char console message !") DG2("THIS CHAR WILL SHOW ON MESSAGE BOX !")End Sub
Public Class deleg Public Sub eventchar(ByVal st As String) Console.WriteLine("delegate sucessfully !" & st) End Sub Public Sub eventmess(ByVal st As String) MsgBox("delegate successfully ! " & st) End Sub End ClassEnd Module一对多代理:
Module Module1
Sub Main()
Dim DC1 As New DelegateCenter() DC1.DelReg(AddressOf delevent1, True) DC1.DelReg(AddressOf delevent2, False) DC1.DelReg(AddressOf delevent3, True) DC1.exefunc(300) Console.ReadLine()End Sub
Public Sub delevent1(ByVal st As String)
Console.WriteLine("delegate event 1 will be start !") End SubPublic Sub delevent2(ByVal st As String)
Console.WriteLine("delegate event 2 will be start !") End SubPublic Sub delevent3(ByVal st As String)
Console.WriteLine("delegate event 3 will be start !") End SubPublic Class DelegateCenter
Public Delegate Sub INRegFunc(ByVal st As String) Private m_customer As INRegFunc Public Sub DelReg(ByVal DG1 As INRegFunc, ByVal decrib As Boolean) If decrib Then m_customer = CType(System.Delegate.Combine(m_customer, DG1), INRegFunc) End If End SubPublic Sub exefunc(ByVal st As String)
m_customer("run successfully ...") End Sub End ClassEnd Module