Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

  • Leaders
Posted

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.

  • *Experts*
Posted

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 Sub

The sub would then be accessed by

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:

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:

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.

  • *Experts*
Posted
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 Sub

No need for multiple handlers. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...