-
Aug 17th, 2012, 02:49 AM
#1
Thread Starter
New Member
Help needed in shortening code
Hey guys,
So i'm a beginner at coding and I attempted to code code in order to do selected things if the check boxes are checked.
I have a total of 4 check boxes in my display, i need it so when 1 or 2 are selected it does the task already defined. If more than 2 are selected I would like it to come up with a message but unsure how to do that within the code i already have.
HTML Code:
Private Sub popping()
If cbfirst.Checked = True Then
If cbsecond.Checked = False Then
If cbthird.Checked = False Then
If cbforth.Checked = False Then
firstpop()
firstpop()
End If
End If
End If
End If
If cbsecond.Checked = True Then
If cbfirst.Checked = False Then
If cbthird.Checked = False Then
If cbforth.Checked = False Then
secondpop()
secondpop()
End If
End If
End If
End If
If cbsecond.Checked = True Then
If cbfirst.Checked = True Then
If cbthird.Checked = False Then
If cbforth.Checked = False Then
firstpop()
secondpop()
End If
End If
End If
End If
If cbsecond.Checked = True Then
If cbfirst.Checked = False Then
If cbthird.Checked = True Then
If cbforth.Checked = False Then
secondpop()
thirdpop()
End If
End If
End If
End If
If cbsecond.Checked = True Then
If cbfirst.Checked = False Then
If cbthird.Checked = False Then
If cbforth.Checked = True Then
secondpop()
forthpop()
End If
End If
End If
End If
If cbsecond.Checked = False Then
If cbfirst.Checked = True Then
If cbthird.Checked = False Then
If cbforth.Checked = True Then
forthpop()
firstpop()
End If
End If
End If
End If
If cbsecond.Checked = False Then
If cbfirst.Checked = False Then
If cbthird.Checked = True Then
If cbforth.Checked = True Then
thirdpop()
forthpop()
End If
End If
End If
End If
If cbsecond.Checked = False Then
If cbfirst.Checked = False Then
If cbthird.Checked = True Then
If cbforth.Checked = False Then
thirdpop()
thirdpop()
End If
End If
End If
End If
If cbsecond.Checked = False Then
If cbfirst.Checked = False Then
If cbthird.Checked = False Then
If cbforth.Checked = True Then
forthpop()
forthpop()
End If
End If
End If
End If
If cbsecond.Checked = False Then
If cbfirst.Checked = True Then
If cbthird.Checked = True Then
If cbforth.Checked = False Then
thirdpop()
firstpop()
End If
End If
End If
End If
Delivery_Stack(Stack_Top) = "Empty"
Stack_Top = Stack_Top - 1
CopydeliveryStack()
End Sub
If you require more of my code or information just ask.
Thanks for your help.
-
Aug 17th, 2012, 05:02 AM
#2
Re: Help needed in shortening code
Something along these lines:
vb.net Code:
Dim checkBoxes = {cbfirst, cbsecond, cbthird, cbfourth} Dim actionCount = 0 Select Case checkBoxes.Count(Function(cb) cb.Checked) Case 0 'No CheckBoxes are checked. Case 1 'Only one CheckBox is checked so perform a single action twice. actionCount = 2 Case 2 'Two CheckBoxes are checked so perform two actions once each. actionCount = 1 Case Else 'More than two CheckBoxes are checked so display error and exit. MessageBox.Show("Error") Return End Select Dim allActions = {New Action(AddressOf firstpop), New Action(AddressOf secondpop), New Action(AddressOf thirdpop), New Action(AddressOf fourthpop)} Dim actionsToPerform As New List(Of Action) For i = 0 To checkBoxes.GetUpperBound(0) If checkBoxes(i).Checked Then For j = 1 To actionCount actionsToPerform.Add(allActions(i)) Next End If Next For Each actionToPerform In actionsToPerform actionToPerform.Invoke() Next
Last edited by jmcilhinney; Aug 17th, 2012 at 05:09 AM.
-
Aug 17th, 2012, 06:37 PM
#3
Thread Starter
New Member
Re: Help needed in shortening code
Originally Posted by jmcilhinney
Something along these lines:
vb.net Code:
Dim checkBoxes = {cbfirst, cbsecond, cbthird, cbfourth}
Dim actionCount = 0
Select Case checkBoxes.Count(Function(cb) cb.Checked)
Case 0
'No CheckBoxes are checked.
Case 1
'Only one CheckBox is checked so perform a single action twice.
actionCount = 2
Case 2
'Two CheckBoxes are checked so perform two actions once each.
actionCount = 1
Case Else
'More than two CheckBoxes are checked so display error and exit.
MessageBox.Show("Error")
Return
End Select
Dim allActions = {New Action(AddressOf firstpop), New Action(AddressOf secondpop), New Action(AddressOf thirdpop), New Action(AddressOf fourthpop)}
Dim actionsToPerform As New List(Of Action)
For i = 0 To checkBoxes.GetUpperBound(0)
If checkBoxes(i).Checked Then
For j = 1 To actionCount
actionsToPerform.Add(allActions(i))
Next
End If
Next
For Each actionToPerform In actionsToPerform
actionToPerform.Invoke()
Next
Will this code work, in a way that includes but is not confined to, when checkbox1 is pressed it only does the action "firstpop" (two times of course) for example. And the same when checkbox 2 and 4 are checked it only does the action "secondpop" and "forthpop".
-
Aug 17th, 2012, 08:30 PM
#4
Re: Help needed in shortening code
Any particular reason that you can't just try it for yourself and see what happens? Any particular reason you can't read the comments in the code, which answer that question specifically?
-
Aug 17th, 2012, 10:16 PM
#5
Thread Starter
New Member
Re: Help needed in shortening code
Originally Posted by jmcilhinney
Any particular reason that you can't just try it for yourself and see what happens? Any particular reason you can't read the comments in the code, which answer that question specifically?
Ill be able to test it soon. [Tested and it works but for a reason unknown to me i couldnt dim the things in the public class it had to be within the same sub as the code, but thank you]
I read the comments in the code and i see how it does the action once or twice and stuff but i dont see how it is able to decipher which checkbox is checked, to me (the beginner i am) it seems like it just tallies how many are checked.
[I understand it more now, last time i read it I was tired or something, sorry]
Last edited by freekygun; Aug 17th, 2012 at 10:22 PM.
-
Aug 17th, 2012, 10:28 PM
#6
Thread Starter
New Member
Re: Help needed in shortening code
Fixed. All working for now. Thank You
Last edited by freekygun; Aug 17th, 2012 at 10:37 PM.
-
Aug 17th, 2012, 11:41 PM
#7
Lively Member
Re: Help needed in shortening code
op's code looked pretty cool though...
matrix + double helix + code =
-
Aug 18th, 2012, 12:40 AM
#8
Re: Help needed in shortening code
Originally Posted by freekygun
Ill be able to test it soon. [Tested and it works but for a reason unknown to me i couldnt dim the things in the public class it had to be within the same sub as the code, but thank you]
You can declare the 'checkBoxes' array variable at the class level and you can even create the array where you declare the variable if you want but you cannot populate it there because, at that point, the CheckBox objects themselves haven't been created. As such, you would just be populating the array with Nothing. If you want to use a member variable then you have to actually populate the array in the Load event handler. The 'allActions' array is fine to be declared, created and populated all in the same place at the class level.
Originally Posted by freekygun
I read the comments in the code and i see how it does the action once or twice and stuff but i dont see how it is able to decipher which checkbox is checked, to me (the beginner i am) it seems like it just tallies how many are checked.
[I understand it more now, last time i read it I was tired or something, sorry]
It seems like you've examined and mostly deciphered the code, which is good. To be clear, it first counts the number of CheckBoxes where the Checked property returns True in order to decide how to proceed. Once it decides that it can proceed, it uses what are called concurrent arrays, i.e. two arrays of the same Length where there is an implied correspondence between elements at the same index in each. It loops through the array of CheckBoxes and, for each one that is checked, it adds the corresponding action to a list. The action is added the number of times determined earlier based on the number of CheckBoxes that are checked. Finally, it loops through the list of actions and executes each one. Just note that you could probably do without that last list of actions and just execute the appropriate action in the preceding loop.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|