Jus wanted to make sure I understood events and delagates correctly, I created this very silly test program (asp.net)
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MyBell As New Bell() Dim myE As EventArgs Dim mypic As New Piccolo() AddHandler MyBell.Ring, AddressOf mypic.Responding_OnRing MyBell.OnRing(myE) End Sub End Class Public Class Bell Public Event Ring As EventHandler Public Overridable Sub OnRing(ByVal e As EventArgs) RaiseEvent Ring(Me, e) End Sub End Class Public Class Piccolo Public Sub Responding_OnRing(ByVal sender As Object, ByVal e As EventArgs) End Sub End Class
I have two classes, the bell and the piccolo (hehe)
* I define a standard event delegate called "Ring" in the Bell
class
* I create a method that raises the event (OnRing)
* Then I create Piccolo class which should respond to the event
* I define an event handler method (Responding_OnRing)
* And finally attach the event to the piccolo objects eventhandler
Is this the preferred way to do it? What if I wanted to write "on the way" on the webpage?? Do I have to write a new delegate that is capable of returning a string, instead of the standard EventHandler delegate? I tried to define a new delegate that handles return values but it failed when I tried to create the event, am I thinking wrong about this? Shouldn't event handlers be capable of returning values? In this case I want to piccolo to re
VB Code:
Public Delegate Function MyEventHandler(ByVal sender As Object, ByVal e As EventArgs) As String
Whats up with the "MyBell.OnRing(myE)"... why do I have to pass an EventArg? Which one should I pass? In the code I create a new! I do not fully understand this part... care to explain?
kind regards
Henrik - trying to learn all about events and delegates in asp.net




Reply With Quote