Results 1 to 8 of 8

Thread: VB - Shorter code?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2003
    Location
    Greece, Salonica
    Posts
    473

    VB - Shorter code?

    OK im sure that this is can get shorter :
    VB Code:
    1. If lstSeeds.ListCount < 16 Then
    2.         MsgBox "Not Enough Seeds", vbOKOnly, "Not Enough Seeds"
    3.     Else
    4.         If lstSeeds.ListCount > 16 Then
    5.             MsgBox "Too Many Seeds", vbOKOnly, "Too Many Seeds"
    6.         Else
    7.             If lstSeeds.ListCount = 16 Then
    8.                 If lstLadder.ListCount > 8 Then
    9.                     MsgBox "Too Many Ladder Rankings", vbOKOnly, "Too Many Ladder Rankings"
    10.                 Else
    11.                     If lstLadder.ListCount < 8 Then
    12.                         MsgBox "Not Enough Ladder Rankings", vbOKOnly, "Not Enough Ladder Rankings"
    13.                     Else
    14.                         If lstLadder.ListCount = 8 Then
    15.                             If lstWildcards.ListCount < 8 Then
    16.                                     MsgBox "Not Enough Wildcards", vbOKOnly, "Not Enough Wildcards"
    17.                                 Else
    18.                                     If lstWildcards.ListCount > 8 Then
    19.                                         MsgBox "Too Many Wildcards", vbOKOnly, "Too Many Wildcards"
    20.                                     Else
    21.                                         If lstWildcards.ListCount = 8 Then
    22.                                             frmBracket.Show
    23.                                         End If
    24.                                     End If
    25.                              End If
    26.                         End If
    27.                     End If
    28.                 End If
    29.             End If
    30.         End If
    31.     End If

    Any ideas?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    First, I think VB has an Elsif or Elseif keyword that you can use to get rid of the useless indentation and a lot of End Ifs.

    Then, you have a few redundant ifs. If you first test x < n, then x > n, then you don't need to test x == n in the final case, as it's guaranteed to be true. This applies to these three lines:
    If lstSeeds.ListCount = 16 Then
    If lstLadder.ListCount = 8 Then
    If lstWildcards.ListCount = 8 Then
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB - Shorter code?

    basically the main improvement can be made by using Select Case statements - which is basically a block of "If"s that check the same variable.

    Also, you were doing checks that werent needed (like "If lstSeeds.ListCount = 16 Then"), as they must be true for them to be run in the first place (as you have already eliminated <16 and >16).

    VB Code:
    1. Select Case lstSeeds.ListCount
    2.     Case < 16:   MsgBox "Not Enough Seeds", vbOKOnly, "Not Enough Seeds"
    3.     Case > 16:   MsgBox "Too Many Seeds", vbOKOnly, "Too Many Seeds"
    4.     Case Else '(by now you already know it must be 16!)
    5.          Select Case lstLadder.ListCount
    6.          Case > 8:  MsgBox "Too Many Ladder Rankings", vbOKOnly, "Too Many Ladder Rankings"
    7.          Case < 8:  MsgBox "Not Enough Ladder Rankings", vbOKOnly, "Not Enough Ladder Rankings"
    8.          Case Else
    9.              Select Case lstWildcards.ListCount
    10.              Case < 8:  MsgBox "Not Enough Wildcards", vbOKOnly, "Not Enough Wildcards"
    11.              Case > 8:  MsgBox "Too Many Wildcards", vbOKOnly, "Too Many Wildcards"
    12.              Case Else
    13.                                frmBracket.Show
    14.              End Select
    15.          End Select
    16.     End Select

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Originally posted by CornedBee
    First, I think VB has an Elsif or Elseif keyword that you can use to get rid of the useless indentation and a lot of End Ifs.
    True, you could have this:
    VB Code:
    1. If lstSeeds.ListCount < 16 Then
    2.     MsgBox "Not Enough Seeds", vbOKOnly, "Not Enough Seeds"
    3. ElseIf lstSeeds.ListCount > 16 Then
    4.     MsgBox "Too Many Seeds", vbOKOnly, "Too Many Seeds"
    5. Else
    6.     If lstLadder.ListCount > 8 Then
    7.         MsgBox "Too Many Ladder Rankings", vbOKOnly, "Too Many Ladder Rankings"
    8.     ElseIf lstLadder.ListCount < 8 Then
    9.         MsgBox "Not Enough Ladder Rankings", vbOKOnly, "Not Enough Ladder Rankings"
    10.     Else
    11. ....
    I think that the Select Case version is much more readable though.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Not for me, I'm not used to expressions being allowed in a multiple-branch structure.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Addicted Member
    Join Date
    Oct 2003
    Posts
    149

    shorter code

    You should use select case because select case is fast and gains speed over nested ifs the more ifs you use. A select case is implemented as a table of pointers to all the label names you use so its real fast.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's not implemented as such if the compiler can't translate it to that. Which is the case here, I'm pretty sure.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    how bout this

    VB Code:
    1. If lstSeeds.ListCount <> 16 Then
    2.     MsgBox "please enter 16 seeds"
    3. ElseIf lstLadder.ListCount <> 8 Then
    4.     MsgBox "please enter 8 rankings"
    5. ElseIf lstWildcards.ListCount <> 8 Then
    6.     MsgBox "please enter 8 wildcards"
    7. Else
    8.     frmBracket.Show
    9. End If

    in the original code you tell the user they've entered too much or too little, but make them guess the actual number youre looking for

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