Results 1 to 19 of 19

Thread: [RESOLVED] Select Case Statement Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    136

    Resolved [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..

    Code:
    If .Top <= 0 And (.Height < UserControl.Height) Then
       msgbox ""
    ElseIf .Top <= 0 And .Height > UserControl.Height Then
       msgbox ""
    End If

  2. #2
    New Member
    Join Date
    Apr 2009
    Posts
    6

    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

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    136

    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("")

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Select Case Statement Question

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

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Select Case Statement Question

    The .Top is never changing. You should better use SELECT.. CASE on the .Height property.
    vb Code:
    1. If .Top <= 0 Then
    2.     Select Case .Height
    3.         Case Is < UserControl.Height
    4.             MsgBox("")
    5.         Case Is > UserControl.Height
    6.             MsgBox("")
    7.     End Select
    8. End If
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Select Case Statement Question

    Quote Originally Posted by Pradeep1210 View Post
    The .Top is never changing. You should better use SELECT.. CASE on the .Height property.
    vb Code:
    1. If .Top <= 0 Then
    2.     Select Case .Height
    3.         Case Is < UserControl.Height
    4.             MsgBox("")
    5.         Case Is > UserControl.Height
    6.             MsgBox("")
    7.     End Select
    8. 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!!!
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Select Case Statement Question

    Quote Originally Posted by Ellis Dee View Post
    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

  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  14. #14
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Select Case Statement Question

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

  15. #15
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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.

  16. #16
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Select Case Statement Question

    Quote Originally Posted by Ellis Dee View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Select Case Statement Question

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

  18. #18
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim loopCount As Long = 100000000
    3.     Dim ctl As Control = Me
    4.     Dim tSCStart, tSCStop, tIfStart, tIfStop As Decimal
    5.  
    6.     tSCStart = Now.Ticks
    7.     For i As Integer = 1 To loopCount
    8.         SelectCaseTest(ctl)
    9.     Next
    10.     tSCStop = Now.Ticks
    11.     Debug.WriteLine("Select Case: " & tSCStop - tSCStart)
    12.  
    13.     Application.DoEvents()
    14.     tIfStart = Now.Ticks
    15.     For i As Integer = 1 To loopCount
    16.         IfElseIfEndIfTest(ctl)
    17.     Next
    18.     tIfStop = Now.Ticks
    19.     Debug.WriteLine("If... EndIf: " & tIfStop - tIfStart)
    20. End Sub
    21.  
    22. Private Sub SelectCaseTest(ByVal c As Control)
    23.     Select Case True
    24.         Case TypeOf c Is TextBox
    25.         Case TypeOf c Is ComboBox
    26.         Case TypeOf c Is ListBox
    27.         Case TypeOf c Is CheckBox
    28.         Case TypeOf c Is RadioButton
    29.     End Select
    30. End Sub
    31.  
    32. Private Sub IfElseIfEndIfTest(ByVal c As Control)
    33.     If TypeOf c Is TextBox Then
    34.     ElseIf TypeOf c Is ComboBox Then
    35.     ElseIf TypeOf c Is ListBox Then
    36.     ElseIf TypeOf c Is CheckBox Then
    37.     ElseIf TypeOf c Is RadioButton Then
    38.     End If
    39. 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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  19. #19

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