包含vb.net委托什么意思的词条

vb.net中如何用事件和委托,会C#中的事件和委托,但不知VB.net中的语法,望给个简单的例子熟悉语法 。一委托vb.net委托什么意思:此示例演示如何将方法与委托关联然后通过委托调用该方法 。
创建委托和匹配过程
创建一个名为 MySubDelegate vb.net委托什么意思的委托 。
Delegate Sub MySubDelegate(ByVal x As Integer)
声明一个类vb.net委托什么意思 , 该类包含与该委托具有相同签名的方法 。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: "CStr(x))
End Sub
End Class
定义一个方法,该方法创建该委托的实例并通过调用内置的 Invoke 方法调用与该委托关联的方法 。
Protected Sub DelegateTest()
Dim c1 As New class1
' Create an instance of the delegate.
Dim msd As MySubDelegate = AddressOf c1.Sub1
' Call the method.
msd.Invoke(10)
End Sub
二、事件
下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件 。AlarmClock 类定义公共事件 Alarm , 并提供引发该事件的方法 。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据 。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法 。AlarmDriver 类一起使用类 , 将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件 。
该示例程序使用事件和委托和引发事件中详细说明的概念 。
示例
' EventSample.vb.
'
Option Explicit
Option Strict
Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Namespace EventSample
' Class that contains the data for
' the alarm event. Derives from System.EventArgs.
'
Public Class AlarmEventArgs
Inherits EventArgs
Private _snoozePressed As Boolean
Private nrings As Integer
'Constructor.
'
Public Sub New(snoozePressed As Boolean, nrings As Integer)
Me._snoozePressed = snoozePressed
Me.nrings = nrings
End Sub
' The NumRings property returns the number of rings
' that the alarm clock has sounded when the alarm event
' is generated.
'
Public ReadOnly Property NumRings() As Integer
Get
Return nrings
End Get
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public ReadOnly Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
End Property
' The AlarmText property that contains the wake-up message.
'
Public ReadOnly Property AlarmText() As String
Get
If _snoozePressed Then
Return "Wake Up!!! Snooze time is over."
Else
Return "Wake Up!"
End If
End Get
End Property
End Class
' Delegate declaration.
'
Public Delegate Sub AlarmEventHandler(sender As Object, _
e As AlarmEventArgs)
' The Alarm class that raises the alarm event.
'
Public Class AlarmClock
Private _snoozePressed As Boolean = False
Private nrings As Integer = 0
Private stopFlag As Boolean = False
' The Stop property indicates whether the
' alarm should be turned off.
'
Public Property [Stop]() As Boolean
Get
Return stopFlag
End Get
Set
stopFlag = value
End Set
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
Set
_snoozePressed = value
End Set
End Property
' The event member that is of type AlarmEventHandler.
'
Public Event Alarm As AlarmEventHandler
' The protected OnAlarm method raises the event by invoking
' the delegates. The sender is always this, the current instance
' of the class.
'
Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
RaiseEvent Alarm(Me, e)
End Sub
' This alarm clock does not have
' a user interface.
' To simulate the alarm mechanism it has a loop
' that raises the alarm event at every iteration
' with a time delay of 300 milliseconds,
' if snooze is not pressed. If snooze is pressed,
' the time delay is 1000 milliseconds.
'
Public Sub Start()
Do
nrings= 1
If stopFlag Then
Exit Do
Else
If _snoozePressed Then
System.Threading.Thread.Sleep(1000)
If (True) Then
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
Else
System.Threading.Thread.Sleep(300)
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
End If
Loop
End Sub
End Class
' The WakeMeUp class has a method AlarmRang that handles the
' alarm event.
'
Public Class WakeMeUp
Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)
Console.WriteLine((e.AlarmTextControlChars.Cr))
If Not e.SnoozePressed Then
If e.NumRings Mod 10 = 0 Then
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Press Snooze? Enter N")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
If input.Equals("N") Or input.Equals("n") Then
CType(sender, AlarmClock).SnoozePressed = True
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End If
Else
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End Sub
End Class
' The driver class that hooks up the event handling method of
' WakeMeUp to the alarm event of an Alarm object using a delegate.
' In a forms-based application, the driver class is the
' form.
'
Public Class AlarmDriver
Public Shared Sub Main()
' Instantiates the event receiver.
Dim w As New WakeMeUp()
' Instantiates the event source.
Dim clock As New AlarmClock()
' Wires the AlarmRang method to the Alarm event.
AddHandler clock.Alarm, AddressOf w.AlarmRang
clock.Start()
End Sub
End Class
End Namespace
vb.net 中在模块(module)里如何实现委托委托三个步骤
1、声明委托用Delegate 声明一个委托 类型参数要和 被委托的方法一样例如Delegate Function a(byval x as string)as string
2、实例化委托dimt as new a(AddressOfFunction 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 (xy)
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
vb.net 中的异步委托机制 , 最好有相关的详细资料讲解,我是新手所谓的委托(Delegate)实际上就是和C/C里面的函数指针差不多,只是增强了类型检查等其它健壮性方面的内容 。异步调用的回调函数有格式要求,所谓格式要求就是参数数量及类型顺序的要求,具体是什么样的你要看文档了 。一般.NETFramework里面都是AsyncCallBack 。
所谓异步编程,就是说你要求做某样事情,但是在完成这件事之前,我能接着做下一件事 , 而当这件事情完成之后,能够有一种机制通知我完成了 。相反,在完成之前一直等待,直到完成了才能进行下一步操作 , 叫做同步 。一般来说,我们平常写的程序都是“同步”,或者成为“顺序执行”更加贴切,而“异步”则可以说是“乱序执行”的 。
可以看到,同步的代码非常好写,因为我们可以预测执行的顺序和情况 。而异步就不是很好写了 , 因为无法得知完成的时候我正在做什么、做到什么程度 。过去写这些代码是比较麻烦的,实现的办法就是自己建立一个处理异步事物的线程,然后在这个线程和主线程之间建立联系 。而现在这个过程大部分已经被系统封装起来了 , 你只要调用BeginXXX,系统就会为你自动建立一个新的线程处理这个事情,当前线程不阻塞,可以马上进行下一项操作,于是就实现了“异步”了 。但是从前面我讲道的你就应该知道,开始异步操作并没有完事,还需要能够得知操作完成,并能够进行相应的处理 。于是你在调用BeginXXX的时候就需要传递一个回调函数,在.NET里是以委托的方式传递的 。回调函数的意思就是“回过头来调用你”,或者说A调用B并且传递函数C的地址,于是B在指定的情况下调用A指定的函数C 。现在就应该明白这个回调函数在BeginXXX中的作用就是 , 当你指定的事情做完之后将会调用这个回调函数 。
在这个回调函数里面,我们就可以进行一些后续的工作,例如接着进行性质相同的工作,或者相应的处理 。在这里,我们也许向知道刚才那件事情执行的情况和结果,这个时候我们就可以通过EndXXX来获得这些东西 。说到这里,结合上面说到的AsyncCallBack以及随便哪个BeginXXX,我们可能会对下列两个东西感到困惑:
IAsyncResult
stateObject
首先说IAsyncResult , 这个是一个接口,你在回调函数中通过参数获得的对象具体是什么类型的一般我们不需要关心,我们只需要依照这个接口的定义进行访问就可以了 。简单点说,这个接口规定了顺利完成异步操作所需信息的最小集合 。一般来说,我们需要通过这个参数(ar)来识别异步操作 。比如说,你在一瞬间发起一百个“从不同的网络连接获取数据”的异步请求,当某一个请求被完成的时候,如何判断是那个请求被完成呢?就是依靠回调函数的参数ar 。实际上你一般是不需要参与判断的,你只要将这个ar传递给EndXXX就可以了,EndXXX会根据这个ar自行判断的 。需要注意的是,这个ar就是你调用BeginXXX的时候的返回值,可以说是一个存根,如果你需要在完成操作前终止他 , 也可以通过将这个存根传递给EndXXX,EndXXX就会根据情况终止操作 。(IAsyncResult里面的IsCompleted提供了是否已经完成的判断,EndXXX就是根据这个值决定是否有必要终止 。当然,你不需要关心他 。)
接下来我们看看stateObject,这个东西被称为状态对象 。于是大家就可能奇怪了:那个ar不也是状态吗?实际上stateObject是一个留给用户使用的东西,BeginXXX/EndXXX根本就不使用 。这个stateObject会被装到ar的AsyncState里面,也就是说这个stateObject可以随时通过访问存根(BeginXXX的返回值)或者回调函数里的ar得到,你完全没必要额外的保存到什么地方,更不需要费神的去考虑如何和你的某个异步操作对应起来 。说了半天,这个东西有什么用呢?你想怎么用就怎么用呗,发挥一下你的想象力 。比如说,你可以保存这是第几次操作,或者在多个异步操作之间要同步的时候可以作为信号旗,再或者直接是操作这个异步操作的对象(x.BeginXXX的时候将x传递到stateObject参数上) 。
第一种用法有点多余 , 第二种用法有点复杂,第三种我用得最多 。因为你很可能在协一个服务端,而服务段必须能够响应多个客户端,这决定了必须用异步 。同时,对于多个客户端必然有多个对象,例如网络连接的时候可能是Socket 。而实际上处理的方法或者协议是和具体哪个客户端没有关联的,因此我们只需要一套处理程序就够了 。这个时候第三种用法就很有用处 , 我们完全可以把代码写成这样:
SubDataReceived(ByValarAsIAsyncResult)
ar.AsyncState.EndReceive(ar)
ar.AsyncState.BeginReceive(...,ar.AsyncState)
EndSub
这样就不需要额外的数据结构去记录有那些正在活动的对象了 。
VB.NET 中事件委托问题address of 顾名思义,就是地址指向 , 每个函数都有一个地址,address of后面加函数名称 。
handels 事件,你看看按钮的单击事件,窗体的启动事件,每个后面都有这个 。
delegate 就是声明一个委托了 。
我也不好详细说,其实你上Baidu搜这几个关键字加上点注解 , 比如“delegate的用法”,N多!
【包含vb.net委托什么意思的词条】vb.net委托什么意思的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、vb.net委托什么意思的信息别忘了在本站进行查找喔 。

    推荐阅读