VB.NET委托的使用

委托三个步骤 

 1、声明委托   用Delegate 声明一个委托 类型  参数要和 被委托的方法一样  例如  Delegate Function a(byval x as string)  as string 

2、实例化委托     dim  t as new a(AddressOf      Function Name)

3.通过 t(参数)   或者  t.Invoke(参数调用委托)

 

 

 

Module module1
    Delegate Function a(ByVal x As Integer, ByVal y As Integer) As Integer    '声明委托类型 委托可以使一个对象调用另一个对象的方法
    Function sum(ByVal x As Integer, ByVal y As Integer) As Integer
        Return (x + y)
    End Function
    Sub main()
        Dim d As New a(AddressOf sum)  '实例化委托
        Dim s = 0
        s = d.Invoke(1, 2)    '执行委托
        Console.WriteLine(s.ToString())
        s = d(1, 2)    '执行委托
        Console.WriteLine(s.ToString())

        MsgBox("")

    End Sub
End Module

 

 

 

 

在UI编程中  比如说我们想用一个函数 处理 多个控件的单击事件 只需要在那个事件响应方法的后面加上 例如

handles button1.click   ,  button2.click  .....来实现

转载于:https://www.cnblogs.com/yuedongwei/archive/2011/11/01/4145609.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值