委托三個(gè)步驟
茌平網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),茌平網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為茌平上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的茌平做網(wǎng)站的公司定做!
1、聲明委托 用Delegate 聲明一個(gè)委托 類型 參數(shù)要和 被委托的方法一樣 例如 Delegate Function a(byval x as string) as string
2、實(shí)例化委托 dim t as new a(AddressOf Function Name)
3.通過 t(參數(shù)) 或者 t.Invoke(參數(shù)調(diào)用委托)
示例:
Module module1
Delegate Function a(ByVal x As Integer, ByVal y As Integer) As Integer '聲明委托類型 委托可以使一個(gè)對象調(diào)用另一個(gè)對象的方法
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) '實(shí)例化委托
Dim s = 0
s = d.Invoke(1, 2) '執(zhí)行委托
Console.WriteLine(s.ToString())
s = d(1, 2) '執(zhí)行委托
Console.WriteLine(s.ToString())
MsgBox("")
End Sub
End Module
多線程中用委托,不會沖突。
在類中用委托,也可以方便模塊思路。
Public Class SP
Public P As New ToolStrip
' Delegate Sub B_CLIKE(tb As Object, e As EventArgs)
' Public Sub ZR(Name As String, BClick As B_CLIKE)
Public Sub ZR(Name As String, BClick As EventHandler)
Dim b As New ToolStripButton
b.Text = Name
b.DisplayStyle = ToolStripItemDisplayStyle.Text
AddHandler b.Click, BClick
' AddHandler b.Click, AddressOf BClick
P.Items.Add(b)
End Sub
End Class
兩個(gè)參數(shù):一個(gè)是數(shù)組名,一個(gè)是傳遞的長度
Function thename(ByVal imgFrom As Image) As String
thename = imgFrom.Name
End Function
Private Sub Image1_Click()
MsgBox thename(Image1)
End Sub