A lot of subs into only one

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 3
Reputation: fulcrum9 is an unknown quantity at this point 
Solved Threads: 0
fulcrum9 fulcrum9 is offline Offline
Newbie Poster

A lot of subs into only one

 
0
  #1
Nov 2nd, 2009
Hello,

I'm a beginner in Visual Basic and I was wondering if it is possible to shorten twenty subs into only 1 sub.

example:
  1. Private Sub box1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
  2. Dim box1Click As Boolean = True
  3. End Sub
  4.  
  5. Private Sub box2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
  6. Dim box2Click As Boolean = True
  7. End Sub
  8.  
  9. ...
  10.  
  11. Private Sub box20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
  12. Dim box20Click As Boolean = True
  13. End Sub

Thank you
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: SecurExpert is an unknown quantity at this point 
Solved Threads: 1
SecurExpert SecurExpert is offline Offline
Newbie Poster
 
0
  #2
Nov 2nd, 2009
No. Each sub in a VB .NET program handles a specific event e.g a click on a button. You cannot collect all the events to be handled in only one sub therefore.

Regards
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 271
Reputation: GeekByChoiCe is on a distinguished road 
Solved Threads: 56
GeekByChoiCe GeekByChoiCe is offline Offline
Posting Whiz in Training
 
1
  #3
Nov 2nd, 2009
actually it is possible. not very sexy but possible...

  1. Private Sub box1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles box1.Click, box2.Click, box3.Click
  2.  
  3. Select Case sender.Name
  4. Case "box1"
  5. 'code for box1
  6. Case "box2"
  7. 'code for box2
  8. Case "box3"
  9. 'code for box3
  10. Case Else
  11.  
  12. End Select
  13.  
  14. End Sub
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 42
Reputation: kplcjl is an unknown quantity at this point 
Solved Threads: 5
kplcjl kplcjl is offline Offline
Light Poster
 
0
  #4
Nov 2nd, 2009
Originally Posted by fulcrum9 View Post
Hello,

I'm a beginner in Visual Basic and I was wondering if it is possible to shorten twenty subs into only 1 sub.

example:
  1. Private Sub box1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
  2. Dim box1Click As Boolean = True
  3. End Sub
  4.  
  5. Private Sub box2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
  6. Dim box2Click As Boolean = True
  7. End Sub
  8.  
  9. ...
  10.  
  11. Private Sub box20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
  12. Dim box20Click As Boolean = True
  13. End Sub

Thank you
1. Your example has "Handles object.Click" where the event is the same object (kist1) When you click on the kist1 object, I believe all the routines would be called just as well as one object. (I've never done so and see no purpose in doing so.)
2. Your examples creates a variable, sets its value, and then discards it without doing anything with it. That doesn't make sense, but you can do multiple things that don't make sense in one routine just as well as 20.
3. Since it passes "ByVal sender As System.Object", this routine is designed to handle multiple events, and is fairly easy to do in C#, but since I am a beginner, I too don't know how to direct multiple objects' one event type to one subroutine. I've got 9 routines that call one routine passing a Byte that tells me which object was called and it does the work. It would have been helpful to know how to do this in VB.NET as well.
PS You can put common logic in one routine and call it 20 times:
  1. Private Sub General_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  2. 'Do something more complex like
  3. Dim btn As System.Windows.Forms.Button
  4. btn = sender
  5. If btn.Name = "Calculate_btn" Then
  6. 'do something
  7. End If
  8. End Sub
  9. Private Sub Calculate1_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate_btn1.Click
  10. General_btn_Click(sender, e)
  11. End Sub
PPS ByVal is a misnomer, no simple object has event handling so ALL senders are passed ByRef.
Last edited by kplcjl; Nov 2nd, 2009 at 11:00 pm. Reason: Trying to make what I said clearer
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 200 | Replies: 3
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC