Entrusted and incident
Entrusted, this word was evil. I thought I had a relationship with the lawyer a few years ago. But in fact, "delegate" is system.delegate
class. It is a class, which means it is a data type and is a reference type. It can reference objects (example methods) and methods of classes (static methods, in VB
SHARED
method).
Use the commission can be summarized as three steps: declaration, instantiation, call.
Public
Class class1
Shang Sub
Main
()
DIM CLS2 AS New Class2
'Instantiation
Dim d1 as onedelegate = addressof cls2.fun1
'transfer
D1 () 'actually performs FUN1
'Instantiation
Dim d2 as onedelegate = addressof class2.fun2
'transfer
D2 () 'actually performs FUN2
Console.readline ()
End Sub
End
Class
'Declaration
Delegate
Sub Onedelegate ()
Public
Class class2
'Instance method
Public Sub Fun1 ()
Console.writeline ("FUN1")
End Sub
'Shared method
Public Shared Sub Fun2 ()
Console.writeline ("FUN2")
End Sub
End
Class
This is nothing to call the delegate. Because the method can be called directly. It is simple to simple. Using the most entrusted is actually an event because the event is implemented by the delegate. Most programs are now driven. To use an event must be correct as follows:
Declaration event
Incident
Write an event handler
Link time handler and events
Declaration event
Usually we all add events to classes when writing a certain class. Please see the following code:
Public
Class class3
'This class has an attribute PROP. Running events after this property changes
'Add event PropChanged
Public Event PropChanged ()
'Add an event to a class, specify that the class can trigger the event.
'To trigger an event, you must use Raisevent, just like this. (In the set of attributes)
'Note: Use the event must be noted that the event cannot have a return value, an optional parameter, paramarray parameter.
Private _prop as string
'Parameter proposition
Public property prop () AS STRING
Get
Return me._prop
END GET
Set (byval value as string)
Me._prop = Value
'When this parameter changes, the event is triggered.
RaiseEvent Propchanged ()
'This happened. But let this event absolutely not
'Final purpose. People use the event mechanism to occur in the event
'When you do something. So, you must use this class.
'Write a function in the program as an event handler.
'Just like class1.cls3_propchanged (then)
End set
End Property
End
Class
Public
Class class1
Shang Sub
Main
()
DIM CLS2 AS New Class2
'Instantiation
Dim d1 as onedelegate = addressof cls2.fun1
'transfer
D1 () 'actually performs FUN1
'Instantiation
Dim d2 as onedelegate = addressof class2.fun2
'transfer
D2 () 'actually performs FUN2
DIM CLS1 AS New Class1
CLS1.TESTEVENT ()
Console.readline ()
End Sub
'Declaration Class3 Object
Private CLS3 AS CLASS3
'Event handler. Prepare PropChanged events for handling object CLS3.
'That is to say, this function is executed after the object cls3.prop is changed.
'This needs to handle the event, which is the method, and the event is linked.
'And then assign values for CLS3.Prop. Just like the TESTEVENT method.
Private subcls3_propchanged ()
'Separate events and event handlers
REMOVEHANDLER CLS3.PropChanged, _
Addressof Me.Cls3_PropChanged
Console.WriteLine ("CLS3_PROPCHANGED is executed)
End Sub
Private sub testEvent ()
Me.cls3 = new class3
'Linking event handles and events.
AddHandler CLS3.Propchanged, Addressof Me.Cls3_Propchanged
'note:
'There is another way to link events and event handler,
'Use WitHevents and Handles. I am very common, so it is not described.
'Assign a value for attributes
Me.cls3.prop = "Change Properties"
'The event occurs at this time, and the event handler is executed.
End Sub
End
Class
'Declaration
Delegate
Sub Onedelegate ()
Public
Class class2
'Instance method
Public Sub Fun1 ()
Console.writeline ("FUN1")
End Sub
'Shared method
Public Shared Sub Fun2 ()
Console.writeline ("FUN2")
End Sub
End
Class
Reference article:
Visual Basic Language Norms - 7.10 Entrusted
Event and commission