Sender as Object, e as EventArgs

emvanlill

Newcomer
Joined
Jun 25, 2003
Messages
10
Location
Ireland
Hi, this may sound stupid, but I would really like to know what the need for 'Sender as Object, e as EventArgs' in a sub routine is. How do you actually pass a value to a sub routine?

Cheers!
Mila:confused:
 
the sender is the name of the object sending the command , eg:
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(sender.name) '/// this would show a message box saying "Button1"
    End Sub

the e is the type of event ( eg : mouse event , general event )
if you did the following :
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(sender.name)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button1_Click(sender, e)
    End Sub
the message box would show as the triggering button , not the actual button name that holds the messagebox code.
 
Those things are simply for the event handlers. When you handle an event, there are always two parameters:

sender is the object which invoked the event. If your event handler only has one object attached to it, it will always be that object. If it has more than one object attached to it (for example, 5 buttons that all use the same event handler), it will depend on the control who's event was triggered.

e is an EventArgs object, or subclass thereof that contains arguments specifically pertaining to that event. Generally, there is a special EventArgs object (inherited from the EventArgs class) for each event. For example, the MouseMove handler will have a MouseEventArgs object, which contains the X and Y coords, the state of the buttons, etc.

These things only apply to event handlers though; you don't need them for regular subroutines. To create a subroutine that takes parameters:
Visual Basic:
Private Sub MySub(ByVal a As Integer)
  MessageBox.Show("You passed " & a.ToString())
End Sub
The sub would then be accessed by
Visual Basic:
MySub(5)
You can also create optional parameters, or parameters of different types using overloading. In addition to the first example I gave, you could create this sub:
Visual Basic:
Private Sub MySub() 'note the lack of any parameters
  MySub(100) 'call the [i]other[/i] MySub with 100 as the parameter
End Sub
Then both of these would be valid:
Visual Basic:
MySub(5) 'shows "You passed 5" in the message box
MySub() 'shows "You passed 100" in the message box
You can read more about subroutines and overloading them in the MSDN.
 
dynamic_sysop: You would be able to achieve the same behaviour with this code:
Visual Basic:
Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
  MessageBox.Show(sender.name) 'depending on which button was clicked, it could show "Button1" or "Button2"
End Sub
No need for multiple handlers. :)
 
Back
Top