Results 1 to 8 of 8

Thread: Help needed in shortening code

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Unhappy 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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Help needed in shortening code

    Something along these lines:
    vb.net Code:
    1. Dim checkBoxes = {cbfirst, cbsecond, cbthird, cbfourth}
    2. Dim actionCount = 0
    3.  
    4. Select Case checkBoxes.Count(Function(cb) cb.Checked)
    5.     Case 0
    6.         'No CheckBoxes are checked.
    7.     Case 1
    8.         'Only one CheckBox is checked so perform a single action twice.
    9.         actionCount = 2
    10.     Case 2
    11.         'Two CheckBoxes are checked so perform two actions once each.
    12.         actionCount = 1
    13.     Case Else
    14.         'More than two CheckBoxes are checked so display error and exit.
    15.         MessageBox.Show("Error")
    16.         Return
    17. End Select
    18.  
    19. Dim allActions = {New Action(AddressOf firstpop), New Action(AddressOf secondpop), New Action(AddressOf thirdpop), New Action(AddressOf fourthpop)}
    20. Dim actionsToPerform As New List(Of Action)
    21.  
    22. For i = 0 To checkBoxes.GetUpperBound(0)
    23.     If checkBoxes(i).Checked Then
    24.         For j = 1 To actionCount
    25.             actionsToPerform.Add(allActions(i))
    26.         Next
    27.     End If
    28. Next
    29.  
    30. For Each actionToPerform In actionsToPerform
    31.     actionToPerform.Invoke()
    32. Next

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Re: Help needed in shortening code

    Quote Originally Posted by jmcilhinney View Post
    Something along these lines:
    vb.net Code:
    1. Dim checkBoxes = {cbfirst, cbsecond, cbthird, cbfourth}
    2. Dim actionCount = 0
    3.  
    4. Select Case checkBoxes.Count(Function(cb) cb.Checked)
    5.     Case 0
    6.         'No CheckBoxes are checked.
    7.     Case 1
    8.         'Only one CheckBox is checked so perform a single action twice.
    9.         actionCount = 2
    10.     Case 2
    11.         'Two CheckBoxes are checked so perform two actions once each.
    12.         actionCount = 1
    13.     Case Else
    14.         'More than two CheckBoxes are checked so display error and exit.
    15.         MessageBox.Show("Error")
    16.         Return
    17. End Select
    18.  
    19. Dim allActions = {New Action(AddressOf firstpop), New Action(AddressOf secondpop), New Action(AddressOf thirdpop), New Action(AddressOf fourthpop)}
    20. Dim actionsToPerform As New List(Of Action)
    21.  
    22. For i = 0 To checkBoxes.GetUpperBound(0)
    23.     If checkBoxes(i).Checked Then
    24.         For j = 1 To actionCount
    25.             actionsToPerform.Add(allActions(i))
    26.         Next
    27.     End If
    28. Next
    29.  
    30. For Each actionToPerform In actionsToPerform
    31.     actionToPerform.Invoke()
    32. 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".

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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?

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Re: Help needed in shortening code

    Quote Originally Posted by jmcilhinney View Post
    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Re: Help needed in shortening code

    Fixed. All working for now. Thank You
    Last edited by freekygun; Aug 17th, 2012 at 10:37 PM.

  7. #7
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    Re: Help needed in shortening code

    op's code looked pretty cool though...

    matrix + double helix + code =

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Help needed in shortening code

    Quote Originally Posted by freekygun View Post
    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.
    Quote Originally Posted by freekygun View Post
    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
  •  



Click Here to Expand Forum to Full Width