VB.NET代理事件

单一代理:

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 Class
End 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 Sub

    Public Sub delevent2(ByVal st As String)
        Console.WriteLine("delegate event 2 will be start !")
    End Sub

    Public Sub delevent3(ByVal st As String)
        Console.WriteLine("delegate event 3 will be start !")
    End Sub

    Public 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 Sub

        Public Sub exefunc(ByVal st As String)
            m_customer("run successfully ...")
        End Sub
    End Class
End Module

 

转载于:https://www.cnblogs.com/MMLoveMeMM/articles/3214819.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值