In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the use of VB.NET events in practical application, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
The constructor of the class in VB.NET is
Sub New ()
End sub
Of course, you can also add parameters. For example, the constructor of the Human class:
Sub New (Byval Name as string
Byval Gender as String, byval
Stature as integer)
Me.Name = Name'me is in VB.NET
Keyword, which represents the object itself, as in java
Me.Gender = this of Gender'.
Super in java is MyBase in VB.NET.
Me.Stature = Stature
End sub
In this way, our Lao Wang
Dim LaoWang As new Human
(Lao Wang, male, 177)
In this case, the Human object must be constructed with parameters. We can add an additional New procedure with no parameters. I skipped it here.
The object is not dead, but alive. The object should be able to actively make some representations to the outside world. This is the event. For example, a person gets sick. At this time, we will take him to the hospital. Let's first define a VB.NET event in the Human class:
Public event FallIll
Let's assume that someone eats too much and gets sick. Write in the Eat process:
Public sub Eat ()
Raiseevent FallIll
'The raiseevent is used to raise an event
End sub
How will the outside world receive this event? Use AddHandler. We need to define a process in sub Main first:
Sub GoToHospital
Console.WriteLine
The patient was taken to the hospital.
End sub
Then bind the process to an event of a specific object:
AddHandler LaoWang.FallIll
AddressOf GoToHospital
In this way, once the LaoWang.Eat is executed, the FallIll event is raised and the GoToHospital procedure is executed. The complete code for the VB.NET event is as follows:
Imports System public module MyModule
Sub Main 'does not call GoToHospital directly in sub Main.
Dim LaoWang as Human
LaoWang = new Human (Lao Wang, male, 177)
AddHandler LaoWang.FallIll
AddressOf GoToHospital
Console.writeline ("{0}, {1})
Height {2} cm ", _
LaoWang.Name, LaoWang.Gender
LaoWang.Stature)
LaoWang.Eat () 'event is raised here
Console.Read
End sub
Sub GoToHospital
Console.WriteLine ("the patient was taken to the hospital.")
End sub
End module
Public class Human
Public Name as String
Public Gender as String
Public Stature as integer
Sub New (Byval Name as string, byval
Gender as String, byval Stature as integer)
Me.Name = Name
Me.Gender = Gender
Me.Stature = Stature
End sub
Sub New () 'constructor with no arguments
End sub
Public event FallIll
Public sub Eat ()
Raiseevent FallIll
End sub
Public sub Sleep ()
End sub
Public sub SeeADoctor ()
End sub
Public function Born () as Human
If Gender = "female" then
Return new Human (",", 50)
Else
Return nothing
End if
End function
End class
VB.NET events can also take parameters. In this way, an event-related object can be attached when the event is raised, so that the catcher of the event can deal with it.
For example, let's change the definition of FallIll to:
Public event FallIll
(Byval Name as String)
Then change the content of Eat to:
Raiseevent FallIll (me.Name)
Give the name of the patient to the captor of the event.
Then change the definition of GoToHospital to:
Sub GoToHospital (Byval Name
As String)
'The number and type of parameters of the process must be captured with
The parameters of the event are the same
Console.WriteLine (Name &
"was taken to the hospital.")
End sub
At this time, we will see: "Lao Wang has been taken to the hospital."
Thank you for reading this article carefully. I hope the article "what is the use of VB.NET events in practical applications" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.