emvanlill Posted July 8, 2003 Posted July 8, 2003 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: Quote
Leaders dynamic_sysop Posted July 8, 2003 Leaders Posted July 8, 2003 the sender is the name of the object sending the command , eg: 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 : 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. Quote
*Experts* Volte Posted July 8, 2003 *Experts* Posted July 8, 2003 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:Private Sub MySub(ByVal a As Integer) MessageBox.Show("You passed " & a.ToString()) End SubThe sub would then be accessed byMySub(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:Private Sub MySub() 'note the lack of any parameters MySub(100) 'call the [i]other[/i] MySub with 100 as the parameter End SubThen both of these would be valid:MySub(5) 'shows "You passed 5" in the message box MySub() 'shows "You passed 100" in the message boxYou can read more about subroutines and overloading them in the MSDN. Quote
*Experts* Volte Posted July 8, 2003 *Experts* Posted July 8, 2003 dynamic_sysop: You would be able to achieve the same behaviour with this code: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 SubNo need for multiple handlers. :) Quote
emvanlill Posted July 8, 2003 Author Posted July 8, 2003 Thanks for the quick reply I appreciate it!!! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.