|
-
Apr 21st, 2009, 11:39 PM
#1
Re: Select Case Statement Question
The Select Case structure can only evaluate one variable. You have two: .Top and .Height. So you need to stick with If...ElseIf chains.
You can hack the Select Case as techgnome showed, but it's a nonstandard implementation -- a hack -- and therefore reduces the maintainability of your code.
-
Apr 22nd, 2009, 12:34 AM
#2
Re: Select Case Statement Question
Why do you consider Select case True a nonstandard hack? Concerning the construct MS says:
A Select Case structure works with a single test expression that is evaluated once, at the top of the structure. Visual Basic then compares the result of this expression with the values for each Case in the structure. If there is a match, it executes the block of statements associated with that Case:
"True" is a test expression (albeit a very simple one) just like any other test expression.
-
Apr 22nd, 2009, 02:46 AM
#3
Re: Select Case Statement Question
 Originally Posted by MartinLiss
"True" is a test expression (albeit a very simple one) just like any other test expression.
Not in this case. Top and Height are the test expressions, not True.
The whole point of the Select Case construct is to evaluate a single expression. The Select Case True hack is conceptually the exact opposite of that, where the series of cases are completely unrelated to one another. That's what makes it a nonstandard hack.
-
Apr 22nd, 2009, 11:49 AM
#4
Re: Select Case Statement Question
 Originally Posted by Ellis Dee
Not in this case. Top and Height are the test expressions, not True.
The whole point of the Select Case construct is to evaluate a single expression. The Select Case True hack is conceptually the exact opposite of that, where the series of cases are completely unrelated to one another. That's what makes it a nonstandard hack.
I won't clutter this thread up any more after this but see the following and many others...
http://www.devx.com/tips/Tip/15288
http://c2.com/cgi/wiki?VbFlexibleSelectCase
http://social.microsoft.com/Forums/e...3-128cd8d551d5
-
Apr 22nd, 2009, 07:41 PM
#5
Re: Select Case Statement Question
 Originally Posted by MartinLiss
This example argues that this is more elegant than If...ElseIf chains:
Code:
Select Case True
Case TypeOf c Is TextBox
Case TypeOf c Is ComboBox
Case TypeOf c Is ListBox
Case TypeOf c Is CheckBox
Case TypeOf c Is OptionButton
End Select
The better way, which is more elegant still:
Code:
Select Case TypeName(c)
Case "TextBox"
Case "ComboBox"
Case "ListBox"
Case "CheckBox"
Case "OptionButton"
End Select
The "BeginsWith" hack is a very nice hack, but it's still a hack. And I still probably wouldn't use that technique. Instead, I'd probably tokenize the line, identify the type of declaration (function, sub, property) using a dedicated function that returns an enumerated declaration type. That enumerated value would then get processed by a Select Case structure.This just looks like you googled "Select Case True" and linked the first three results you found.
I don't dispute that it functions. I'm disputing that it's a best practice. The fact that none of your links are affiliated with Microsoft in any way is pretty good evidence that this technique is, in fact, a nonstandard hack.
-
Apr 23rd, 2009, 03:44 AM
#6
Re: Select Case Statement Question
 Originally Posted by Ellis Dee
This example argues that this is more elegant than If...ElseIf chains:
Code:
Select Case True
Case TypeOf c Is TextBox
Case TypeOf c Is ComboBox
Case TypeOf c Is ListBox
Case TypeOf c Is CheckBox
Case TypeOf c Is OptionButton
End Select
I wonder how that Select...Case structure is superior to the If..ElseIf..End If structure. 
Does anyone see any clear gain here? It doesn't save you with any significant typing work, nor does it add any performance gain etc.
Code:
If TypeOf c Is TextBox Then
ElseIf TypeOf c Is ComboBox Then
ElseIf TypeOf c Is ListBox Then
ElseIf TypeOf c Is CheckBox Then
ElseIf TypeOf c Is OptionButton Then
End If
Last edited by Pradeep1210; Apr 23rd, 2009 at 03:48 AM.
-
Apr 23rd, 2009, 09:05 AM
#7
Re: Select Case Statement Question
 Originally Posted by Pradeep1210
I wonder how that Select...Case structure is superior to the If..ElseIf..End If structure. 
Does anyone see any clear gain here? It doesn't save you with any significant typing work, nor does it add any performance gain etc.
According to a test I just did with GetTickCount the Select Case code is about 10% faster.
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
|