Results 1 to 6 of 6

Thread: Event and delegates

Threaded View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Event and delegates

    Jus wanted to make sure I understood events and delagates correctly, I created this very silly test program (asp.net)

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim MyBell As New Bell()
    3.         Dim myE As EventArgs
    4.         Dim mypic As New Piccolo()
    5.  
    6.         AddHandler MyBell.Ring, AddressOf mypic.Responding_OnRing
    7.         MyBell.OnRing(myE)
    8.  
    9.     End Sub
    10.  
    11. End Class
    12.  
    13. Public Class Bell
    14.     Public Event Ring As EventHandler
    15.  
    16.     Public Overridable Sub OnRing(ByVal e As EventArgs)
    17.         RaiseEvent Ring(Me, e)
    18.     End Sub
    19.  
    20.    
    21. End Class
    22.  
    23. Public Class Piccolo
    24.     Public Sub Responding_OnRing(ByVal sender As Object, ByVal e As EventArgs)
    25.  
    26.     End Sub
    27. 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:
    1. 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
    Last edited by MrNorth; Feb 25th, 2004 at 03:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width