944,108 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 433
  • VB.NET RSS
Nov 2nd, 2009
0

A lot of subs into only one

Expand 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:
VB.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fulcrum9 is offline Offline
3 posts
since Jul 2009
Nov 2nd, 2009
0
Re: A lot of subs into only one
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SecurExpert is offline Offline
24 posts
since Sep 2009
Nov 2nd, 2009
1
Re: A lot of subs into only one
actually it is possible. not very sexy but possible...

vb Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 208
Solved Threads: 168
Practically a Master Poster
GeekByChoiCe is offline Offline
692 posts
since Jun 2009
Nov 2nd, 2009
0
Re: A lot of subs into only one
Click to Expand / Collapse  Quote originally posted by fulcrum9 ...
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:
VB.NET Syntax (Toggle Plain Text)
  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:
VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 14
Solved Threads: 12
Junior Poster
kplcjl is offline Offline
146 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: How to delete a record from a DB?
Next Thread in VB.NET Forum Timeline: help with windows form applications





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC