[RESOLVED] Select Case Statement Question
How to convert this code to SELECT CASE statement? i can't figure out how to use "AND" in the SELECT CASE statement. Maybe somebody can explain briefly to me..:blush: :blush:
Code:
If .Top <= 0 And (.Height < UserControl.Height) Then
msgbox ""
ElseIf .Top <= 0 And .Height > UserControl.Height Then
msgbox ""
End If
Re: Select Case Statement Question
correct me if im wrong but isnt it just
Select Case .top
Case Is <= 0 And (.Height < UserControl.Height)
MsgBox("")
case else <= 0 And .Height > UserControl.Height
MsgBox("")
End Select
Re: Select Case Statement Question
While I don't think it's a candidate for converting to a case statement... it would look something like this:
Code:
Select Case TRue
Case .Top <= 0 And (.Height < UserControlHeight)
....
-tg
Re: Select Case Statement Question
i had tried this code before but doesn't work.
Code:
Select Case .top
Case Is <= 0 And (.Height < UserControl.Height)
MsgBox("")
case else <= 0 And .Height > UserControl.Height
MsgBox("")
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.
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.
Re: Select Case Statement Question
The code in post#2 is invalid.
@techgnome's way is valid but not better than the original [If...] code in post#1.
Another way:
Code:
If .Top <= 0 Then
Select Case .Height - UserControl.Height
Case Is < 0: MsgBox "A"
Case Is > 0: MsgBox "B"
End Select
End If
Re: Select Case Statement Question
Quote:
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.
Re: Select Case Statement Question
The .Top is never changing. You should better use SELECT.. CASE on the .Height property.
vb Code:
If .Top <= 0 Then
Select Case .Height
Case Is < UserControl.Height
MsgBox("")
Case Is > UserControl.Height
MsgBox("")
End Select
End If
Re: Select Case Statement Question
Quote:
Originally Posted by
Pradeep1210
The .Top is never changing. You should better use SELECT.. CASE on the .Height property.
vb Code:
If .Top <= 0 Then
Select Case .Height
Case Is < UserControl.Height
MsgBox("")
Case Is > UserControl.Height
MsgBox("")
End Select
End If
This is very similar to my code in post#7 that I brought UserControl.Height up to Select Case line, by that way the code needs to access to UserControl's properties at maximum of once, perhaps 1 nano second can be saved!!!:p
Re: Select Case Statement Question
I still insist that this isn't a candidate for a select case in the first place.
Just goes to show, ask 6 developers how to do something, you'll get 10 different answers.
-tg
Re: Select Case Statement Question
Quote:
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
Re: Select Case Statement Question
A single Select Case without If...EndIf
Code:
Select Case True
Case .Top > 0
'-- do nothing
Case .Height < UserControl.Height
MsgBox "A"
Case .Height > UserControl.Height
MsgBox "B"
End Select
Re: Select Case Statement Question
Quote:
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.
Re: [RESOLVED] Select Case Statement Question
To be clear, I'm not saying that all hacks are by definition bad and should never be used. I think they're fine if they add significant value. Typically, that means they add noticeable speed improvement inside a critical loop. (Which could be a solid justification for the TypeOf case construct above, though that wasn't the reason given for doing it that way.)
Here is an example of a hack for speed that I might use if I were really hurting for performance in a tight loop. Short of that, I'd avoid Mid's replication feature like the plague.
Another good time to use hacks is when it greatly simplifies a very complex problem. Here is a great example of that. (That problem may be unsolvably complex without the hack solution graciously provided by Merri.)
Other than these two reasons, I would have a hard time justifying the use of a hack. I'm just not seeing the added value of Select Case True.
Re: Select Case Statement Question
Quote:
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. :rolleyes:
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
Re: Select Case Statement Question
Quote:
Originally Posted by
Pradeep1210
I wonder how that Select...Case structure is superior to the If..ElseIf..End If structure. :rolleyes:
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.
Re: [RESOLVED] Select Case Statement Question
Hey Marty,
I did a test for performance and the If..EndIf seems to be much faster compared to Select...Case.
I was using VB.NET though. Maybe that implements it differently.
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim loopCount As Long = 100000000
Dim ctl As Control = Me
Dim tSCStart, tSCStop, tIfStart, tIfStop As Decimal
tSCStart = Now.Ticks
For i As Integer = 1 To loopCount
SelectCaseTest(ctl)
Next
tSCStop = Now.Ticks
Debug.WriteLine("Select Case: " & tSCStop - tSCStart)
Application.DoEvents()
tIfStart = Now.Ticks
For i As Integer = 1 To loopCount
IfElseIfEndIfTest(ctl)
Next
tIfStop = Now.Ticks
Debug.WriteLine("If... EndIf: " & tIfStop - tIfStart)
End Sub
Private Sub SelectCaseTest(ByVal c As Control)
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 RadioButton
End Select
End Sub
Private Sub IfElseIfEndIfTest(ByVal c As Control)
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 RadioButton Then
End If
End Sub
This is what comes up:
Code:
Select Case: 117343750
If... EndIf: 110625000
Select Case: 116875000
If... EndIf: 110468750
Select Case: 116875000
If... EndIf: 110781250
Select Case: 116875000
If... EndIf: 110625000
Select Case: 116718750
If... EndIf: 110468750
Select Case: 117656250
If... EndIf: 110625000
Re: [RESOLVED] Select Case Statement Question
That's .Net and we are talking about VB6.