|
-
Feb 17th, 2004, 10:44 AM
#1
Thread Starter
Hyperactive Member
VB - Shorter code?
OK im sure that this is can get shorter :
VB Code:
If lstSeeds.ListCount < 16 Then
MsgBox "Not Enough Seeds", vbOKOnly, "Not Enough Seeds"
Else
If lstSeeds.ListCount > 16 Then
MsgBox "Too Many Seeds", vbOKOnly, "Too Many Seeds"
Else
If lstSeeds.ListCount = 16 Then
If lstLadder.ListCount > 8 Then
MsgBox "Too Many Ladder Rankings", vbOKOnly, "Too Many Ladder Rankings"
Else
If lstLadder.ListCount < 8 Then
MsgBox "Not Enough Ladder Rankings", vbOKOnly, "Not Enough Ladder Rankings"
Else
If lstLadder.ListCount = 8 Then
If lstWildcards.ListCount < 8 Then
MsgBox "Not Enough Wildcards", vbOKOnly, "Not Enough Wildcards"
Else
If lstWildcards.ListCount > 8 Then
MsgBox "Too Many Wildcards", vbOKOnly, "Too Many Wildcards"
Else
If lstWildcards.ListCount = 8 Then
frmBracket.Show
End If
End If
End If
End If
End If
End If
End If
End If
End If
Any ideas?
-
Feb 17th, 2004, 10:52 AM
#2
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.
-
Feb 17th, 2004, 10:56 AM
#3
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:
Select Case lstSeeds.ListCount
Case < 16: MsgBox "Not Enough Seeds", vbOKOnly, "Not Enough Seeds"
Case > 16: MsgBox "Too Many Seeds", vbOKOnly, "Too Many Seeds"
Case Else '(by now you already know it must be 16!)
Select Case lstLadder.ListCount
Case > 8: MsgBox "Too Many Ladder Rankings", vbOKOnly, "Too Many Ladder Rankings"
Case < 8: MsgBox "Not Enough Ladder Rankings", vbOKOnly, "Not Enough Ladder Rankings"
Case Else
Select Case lstWildcards.ListCount
Case < 8: MsgBox "Not Enough Wildcards", vbOKOnly, "Not Enough Wildcards"
Case > 8: MsgBox "Too Many Wildcards", vbOKOnly, "Too Many Wildcards"
Case Else
frmBracket.Show
End Select
End Select
End Select
-
Feb 17th, 2004, 10:59 AM
#4
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:
If lstSeeds.ListCount < 16 Then
MsgBox "Not Enough Seeds", vbOKOnly, "Not Enough Seeds"
ElseIf lstSeeds.ListCount > 16 Then
MsgBox "Too Many Seeds", vbOKOnly, "Too Many Seeds"
Else
If lstLadder.ListCount > 8 Then
MsgBox "Too Many Ladder Rankings", vbOKOnly, "Too Many Ladder Rankings"
ElseIf lstLadder.ListCount < 8 Then
MsgBox "Not Enough Ladder Rankings", vbOKOnly, "Not Enough Ladder Rankings"
Else
....
I think that the Select Case version is much more readable though.
-
Feb 17th, 2004, 02:27 PM
#5
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.
-
Feb 19th, 2004, 03:45 PM
#6
Addicted Member
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.
-
Feb 22nd, 2004, 09:02 AM
#7
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.
-
Feb 28th, 2004, 04:21 AM
#8
Frenzied Member
how bout this
VB Code:
If lstSeeds.ListCount <> 16 Then
MsgBox "please enter 16 seeds"
ElseIf lstLadder.ListCount <> 8 Then
MsgBox "please enter 8 rankings"
ElseIf lstWildcards.ListCount <> 8 Then
MsgBox "please enter 8 wildcards"
Else
frmBracket.Show
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|