Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement event and delegation under VB.NET

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to implement events and delegates under VB.NET. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

A delegate is an object that can be used to call other object methods. They are sometimes called type-safe function pointers because they are similar to the function pointers used in other programming languages. But unlike function pointers, VB.NET delegates are based on the reference type of the System.Delegate class, which can reference shared methods-methods and instance methods that can be called without a specific class instance.

Delegates are useful in situations where the calling procedure and the called procedure need a medium. For example, you might want an object that raises an event to be able to call different event handlers in different environments. Unfortunately, the object that raised the event cannot know in advance the event handler that handles the particular event. Visual Basic .NET allows you to dynamically associate event handlers with events by creating delegates when using AddHandler statements. At run time, the delegate forwards various calls to the appropriate event handler.

Although you can create your own delegates, in most cases, Visual Basic .NET creates delegates and provides you with specific information. For example, the Event statement implicitly defines a delegate class named EventHandler as a nested class of the class in which the Event statement resides, with the same signature as the event. The AddressOf statement implicitly creates an instance of the delegate. For example, the following two lines of code are equivalent:

AddHandler Button1.Click AddressOf Me.Button1_Click 'AddHandler points to the object that raised the event, and AddressOf determines the event handler to be called by the event object.' the above behavior can also be called monitoring AddHandler Button1.Click, New EventHandler (AddressOf Button1_Click)

As long as the compiler can determine the type of delegate with context, it can use the sketching method to create the delegate.

Note: handle events inherited from the base class

Derived classes (classes that inherit the characteristics of a base class) can use Handles MyBase statements to handle events raised by their base class.

Handling events from the base class

By adding Handles MyBase. Exe to the declaration line of the event handler procedure. Statement to declare an event handler in a derived class, where event name is the name of the event being processed in the base class. For example:

Public Class Class1 Public Event SE (ByVal i As Integer) 'Place methods and properties here. End Class Public Class Class2 Inherits Class1 Sub EventHandler (ByVal x As Integer) Handles MyBase.SE 'Place code to handle events from Class1 here. End Sub End Class

Declare events that use existing delegate types

In some cases, you may want to declare that an event uses an existing delegate type as the base delegate. The following syntax describes how to make a declaration:

Event AnEvent As DelegateType this syntax is useful when routing multiple events to the same handler, but you need to define the following when using an existing delegate type:

Delegate {sub | function} DelegateType (parameter 1, parameter 2)

The following is a vb.net console program that demonstrates a single event delegated multihandler.

For example, in society, you are the law, there are three kinds of people in society, the police, scoundrels and voluntary anti-pickpocketing teams.

You stipulate: if the thief steals, the police will punish the thief 500 yuan, and the voluntary anti-pickpocketing team will fight the scoundrel and go to the hospital once.

This is the real commission.

In fact, in writing programs, programmers are the law, and the police, thieves and voluntary anti-pickpocketing teams are the three objects. The thief steals things is one way, the thief also has a theft incident, he steals things to stimulate this event. The police and the voluntary anti-pickpocketing team are the two targets of the incident. the police are responsible for deducting 500 yuan from the thief, and the voluntary anti-pickpocketing team needs to steal into the hospital once.

Therefore, the delegate has the following elements:

The object of the incident is the thief.

The people who deal with the incident are the police and the voluntary anti-pickpocketing team.

The definition of entrustment is that the law allows the police and voluntary anti-theft teams to monitor thieves.

If all three elements are satisfied, then you have written a complete event handling.

Here is an example of the vb.net console

Imports System Module Module1'is ready to quote the scoundrel theft event Public WithEvents w As rogue Sub Main () Console.WriteLine ("A new day begins.") Example Chinese police, scoundrel, voluntary anti-pickpocketing team Dim p As New police w = New rogue Dim pp As New voluntary anti-pickpocketing team 'set up monitoring of rogue theft incidents and set up corresponding incident handling procedures' if thieves play with stealing, quote the police fine handling program AddHandler w.PlayGame, AddressOf p. Withholding money if the thief steals, he will quote the beating procedure of the voluntary anti-pickpocketing team AddHandler w.PlayGame, AddressOf pp. Hit people w. Stealing () Console.WriteLine ("it's getting dark.") Console.ReadLine () End Sub 'End Class Public Class police Sub New () Console.WriteLine ("generate police.") the parameter type of the event handler defined by End Sub' should be consistent with the parameter type of the defined delegate (ByVal sender As Object, ByVal e As System.EventArgs) Console.WriteLine ("Police: good boy, dare to steal.") 'define thread output delay 3 seconds System.Threading.Thread.Sleep (3000) Console.WriteLine ("policeman: let's see how much money you have.") Dim f As scoundrel = sender System.Threading.Thread.Sleep (3000) Console.WriteLine ("scoundrel money:" + f. Money. ToString () System.Threading.Thread.Sleep (3000) Console.WriteLine ("start deducting money.") System.Threading.Thread.Sleep (3000) f. Money = f. Money-500 Console.WriteLine ("deducted. Now the scoundrel still has:" + f. Money. ToString () System.Threading.Thread.Sleep (3000) End Sub End Class Public Class scoundrel 'first define an event of theft using the existing delegate type PlayGameHandler as the base delegate Public Event PlayGame As PlayGameHandler Private m_Money As Integer Private ey As Integer Sub New () Console.WriteLine ("generate scoundrel....") M_Money = 1000 ey = 5 End Sub 'definition beaten attribute is used to record how many times Property has been beaten () As Integer Get Return ey End Get Set (ByVal Value As Integer) ey = Value End Set End Property' definition before attribute is used to record Property money () As Integer Get Return m_Money End Get Set (ByVal Value As Integer) m_Money = Value End Set End Property 'define stealing process Public Sub stealing () Console.WriteLine ( "scoundrels start stealing") System.Threading.Thread.Sleep (3000) 'instantiate the event parameter Dim e As New System.EventArgs' to trigger a theft event The parameter type should be consistent with the existing delegate type RaiseEvent PlayGame (Me, e) End Sub End Class Public Class voluntary anti-pickpocketing team Sub New () Console.WriteLine ("voluntary anti-pickpocketing team is coming.") The parameter type of the event handler defined by System.Threading.Thread.Sleep (3000) End Sub 'should be consistent with the parameter type of the defined delegate. Public Sub (ByVal sender As Object, ByVal e As System.EventArgs) System.Threading.Thread.Sleep (3000) Console.WriteLine ("Voluntary Anti-pickpocketing team: good boy, dare to steal.") System.Threading.Thread.Sleep (3000) Console.WriteLine ("Voluntary Anti-pickpocketing team: how many times have you been beaten?") Dim bn As scoundrel = sender System.Threading.Thread.Sleep (3000) Console.WriteLine ("rogue:" + bn. Hit .ToString + "times") System.Threading.Thread.Sleep (3000) Console.WriteLine ("Voluntary Anti-pickpocketing team: then I have to hit you, first punch.") System.Threading.Thread.Sleep (3000) Console.WriteLine ("Voluntary Anti-pickpocketing team: one more shot.") System.Threading.Thread.Sleep (3000) Console.WriteLine ("Voluntary Anti-pickpocketing team: it's over. Hurry to the hospital.") End Sub End Class' defines the delegate type Delegate Sub PlayGameHandler (ByVal sender As Object, ByVal e As System.EventArgs) End Module. Thank you for reading! This is the end of the article on "how to realize events and delegation under VB.NET". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report