C#中的委托和拉姆达表达式用VB.net怎么写委托主要用于.NETFramework中vb.net委托实例的事件处理程序和回调函数vb.net委托实例,它是事件的基础 。委托的作用类似于c中函数指针的作用 。不同的是vb.net委托实例 , 委托实例独立于它所封装的方法的类vb.net委托实例,并且方法类型与委托的类型是兼容的 。函数指针只能引用静态函数,而委托可以应用静态和实例方法 。所有委托都是继承自System.Delegate类,并且有一个调用列表 。调用委托时所执行的方法都被存放在这样的一个连接列表中 。使用delegate关键字可以声明一个委托 。通过将委托与命名方法或匿名方法关联,可以对委托进行实例化 。为了与命名方法一起使用,委托必须用具有可接受签名的方法进行实例化 。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1{//声明一个委托delegateintMydelegate();classProgram{staticvoidMain(string[]args){testp=newtest();//将委托指向非静态方法Mydelegatem=newMydelegate(p.InstanceMethod);//调用非静态方法m();//将委托指向静态方法m=newMydelegate(test.StaticMethod);//调用静态方法m();Console.Read();}}publicclasstest{publicintInstanceMethod(){Console.WriteLine("正在调用非静态方法InstanceMethod().");return0;}staticpublicintStaticMethod(){Console.WriteLine("正在调用静态方法StaticMethod() 。。。。");return0;}}}
VB.NET中的多线程和委托是什么关系? 能不能给我一个通俗易懂的范例,谢谢委托,Delegate
就是让你处于这个线程里时,委托另一个线程去执行一些动作
我简单举一个写richtextbox的例子:
////////////////////////////////////////////
'创建一个名为 MySubDelegate 的委托 。
Delegate Sub MySubDelegate(ByVal txt As String)
'写信息到富文本主窗口
Private Sub txtW(ByVal txt As String)
Dim msgd As New MySubDelegate(AddressOf Me.txtW1)
Dim arg(0) As Object
arg(0) = txt
Me.Invoke(msgd, arg)
End Sub
'委托指向
Private Sub txtW1(ByVal txt As String)
Me.RichTextBox1.AppendText(txt)
End Sub
/////////////////////
这样,你在多线程应用时,在其他线程里用txtW(str)来写richtextbox,就不会产生错误了 。不然,直接垮线程写richtextbox,可能会出现和UI线程的冲突 。
vb.net中如何用事件和委托,会C#中的事件和委托,但不知VB.net中的语法,望给个简单的例子熟悉语法 。一委托:此示例演示如何将方法与委托关联然后通过委托调用该方法 。
创建委托和匹配过程
创建一个名为 MySubDelegate vb.net委托实例的委托 。
Delegate Sub MySubDelegate(ByVal x As Integer)
声明一个类vb.net委托实例,该类包含与该委托具有相同签名vb.net委托实例的方法 。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: "CStr(x))
End Sub
End Class
定义一个方法,该方法创建该委托vb.net委托实例的实例并通过调用内置的 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
【vb.net委托实例的简单介绍】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委托实例的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、vb.net委托实例的信息别忘了在本站进行查找喔 。
推荐阅读
- python3获取网页数据,python3抓取网页数据
- 女生各种职业角色扮演游戏,女生各种职业角色扮演游戏叫什么
- 新媒体小白如何写文章赚钱,写好一篇新媒体文章需要哪些能力
- python编码函数 python编码规范有哪些
- mongodb从3.4升级到4.0的简单介绍
- python3.7安装tkinter,Python37安装machine库问题
- 理财直播录屏,直播录屏资源
- c语言中的函数名的表示 c语言函数的命名规范
- 手机什么cpu最好,手机什么cpu最好用