Results 1 to 17 of 17

Thread: [RESOLVED] Select Case True

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    315

    Resolved [RESOLVED] Select Case True

    Is
    Select Case True
    considered an acceptable construct?

    Or should it be avoided?

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,839

    Re: Select Case True

    Doesn't seem to make sense. Why would you ever use a constant expression there?

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,489

    Re: Select Case True

    Depends on who you ask. Yes it's a valid construct. Should it be avoided? Personally I don't like it. But that's my personal opinion. I know there are others here that don't see a problem with it. In the end it's about standards. Decide up front if you're going to allow it or not, and be consistent about it.

    -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
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    315

    Re: Select Case True

    I'm doing this
    Code:
    Private Sub Command1_Click()
    
        Dim tsN As Long
        Dim tsD As Long
        
        tsN = 3
        tsD = 4
    
        Select Case True
            Case (tsN = 3 And tsD = 4)
                MsgBox "TimeSig = 3/4 time"
            Case (tsN = 7 And tsD = 8)
                MsgBox "TimeSig = 7/8 time"
        End Select
    
    End Sub
    and just wondering if OK, or are there implications that I am not aware of.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,704

    Re: Select Case True

    Quote Originally Posted by mms_ View Post
    Is
    Select Case True
    considered an acceptable construct?

    Or should it be avoided?
    Yes I think it is totally valid and has it's place though is often better done a different way.

    Here is one small example of how I might consider using it. Consider a form has multiple option buttons on it and during processing you want to see which is selected.
    One option to do this would be
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Select Case True
    Case Option1
        Debug.Print "Option1 selected"
    Case Option2
        Debug.Print "Option2 selected"
    Case Option3
        Debug.Print "Option3 selected"
    End Select
    
    End Sub
    Last edited by DataMiser; Sep 8th, 2021 at 02:04 PM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    315

    Re: Select Case True

    Actually, this does the trick, and is perhaps even more readable.
    Code:
    Private Sub Command1_Click()
    
        Dim tsN As Long
        Dim tsD As Long
        
        tsN = 3
        tsD = 4
        
        Dim sTimeSig As String
        sTimeSig = tsN & "/" & tsD
    
        Select Case sTimeSig
            Case "3/4"
                MsgBox "TimeSig = 3/4 time"
            Case "7/8"
                MsgBox "TimeSig = 7/8 time"
        End Select
    
    End Sub

  7. #7

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,839

    Re: [RESOLVED] Select Case True

    What is the advantage over If ElseIf ElseIf EndIf?

    I mean, besides its obfuscation factor and likelihood of confusing readers and leading to errors during subsequent maintenance?

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,704

    Re: [RESOLVED] Select Case True

    Quote Originally Posted by dilettante View Post
    What is the advantage over If ElseIf ElseIf EndIf?

    I mean, besides its obfuscation factor and likelihood of confusing readers and leading to errors during subsequent maintenance?
    In the case I showed it is less code and IMO more readable and easier to maintain.

  10. #10
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,403

    Re: [RESOLVED] Select Case True

    It is valid, very useful and readable.

    You can do a select case with any constant, anyway I personally have only used True.

    Code:
    Private Sub Command1_Click()
        Dim a As Long
        Dim b As Long
        
        a = InputBox("a")
        b = InputBox("b")
        
        Select Case 7
            Case a
                MsgBox "a"
            Case b
                MsgBox "b"
            Case a + b
                MsgBox "a + b"
            Case a - b
                MsgBox "a - b"
            Case b - a
                MsgBox "b - a"
            Case Else
                MsgBox "none"
        End Select
    End Sub

  11. #11
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,299

    Re: [RESOLVED] Select Case True

    Quote Originally Posted by dilettante View Post
    What is the advantage over If ElseIf ElseIf EndIf?

    I mean, besides its obfuscation factor and likelihood of confusing readers and leading to errors during subsequent maintenance?
    It does short-circuit OR expressions like If rs Is Nothing Or rs!DataField = 0 Then which bomb out otherwise on second sub-expression.

  12. #12
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    447

    Re: [RESOLVED] Select Case True

    Poor readability
    Code:
        Select Case True
        Case rs Is Nothing
            MsgBox "Hello 1"
        Case rs!DataField = 0
            MsgBox "Hello 2"
        End Select
    Good readability
    Code:
        If rs Is Nothing Then
            MsgBox "Hello 1"
        ElseIf rs!DataField = 0 Then
            MsgBox "Hello 2"
        End If

  13. #13
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    447

    Re: [RESOLVED] Select Case True

    Regarding short-circuit, this is a sad VB6 story

    JavaScript
    Code:
    if (a > b || r && r.default) return e;
    VB6
    Code:
    If a > b Then
        Set result = e
        Exit Function
    End If
    
    If Not r Is Nothing Then
        If Not r.Default Is Nothing Then
            Set result = e
            Exit Function
        End If
    End If

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,704

    Re: [RESOLVED] Select Case True

    Quote Originally Posted by SearchingDataOnly View Post
    Poor readability
    Code:
        Select Case True
        Case rs Is Nothing
            MsgBox "Hello 1"
        Case rs!DataField = 0
            MsgBox "Hello 2"
        End Select
    Good readability
    Code:
        If rs Is Nothing Then
            MsgBox "Hello 1"
        ElseIf rs!DataField = 0 Then
            MsgBox "Hello 2"
        End If
    In a case where there is only one or two elseif statements I may go that route myself but when you get more case becomes more of an option. Keep in mind that a case block does the same thing as multiple else ifs but is a little less code and can be easier to read. Assuming of course you have no trouble reading case statements.

    Let's say you have 10 conditions. You could use 1 If then followed by 9 elseif then followed by a else or you could have a select case with 10 cases and an else. The latter is cleaner and fewer total characters needed.

  15. #15
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,493

    Re: [RESOLVED] Select Case True

    I only use Select Case if theres "minimum" 3 different routes.
    also the usage of string to compare numbers is not efficient.

    if tsN = 3 and tsD = 4 then MsgBox "TimeSig = 3/4 time"
    if tsN = 7 and tsD = 8 then MsgBox "TimeSig = 7/8 time"

    just a few if's should be faster.
    also, if theres multiple routes u could also do:

    Select Case tsN * 1000 + tsD
    Case 3004: MsgBox "TimeSig = 3/4 time"
    Case 7008: MsgBox "TimeSig = 7/8 time"
    Case 12060: MsgBox "TimeSig = 12/60 time"
    End Select

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,299

    Re: [RESOLVED] Select Case True

    Quote Originally Posted by SearchingDataOnly View Post
    Poor readability
    Code:
        Select Case True
        Case rs Is Nothing
            MsgBox "Hello 1"
        Case rs!DataField = 0
            MsgBox "Hello 2"
        End Select
    Good readability
    Code:
        If rs Is Nothing Then
            MsgBox "Hello 1"
        ElseIf rs!DataField = 0 Then
            MsgBox "Hello 2"
        End If
    There is no MsgBox “Hello 2” in the short-circuit OR sample above so it has to be the same code in both cases.

    There might be more than 2 sub-expresions like 10 of them.

    The body might be more than single line MsgBox like 100 of lines of code.

    And finally the replacement syntax is

    Select Case True
    Case Expr1, Expr2, Expr3, . . .
    Stmt1
    Stmt2
    . . .
    End Select

    so body is not repeated and snippet is overall one line longer than the original.

  17. #17
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,715

    Re: [RESOLVED] Select Case True

    I use Select Case True quite a bit, and see no problems with it at all. However, if I can identify a single variable for the Select Case [test], I will use that. But, sometimes, the conditions just don't work out quite that way. As stated above, we must remember that the first True Case it finds will stop any further testing.


    ***************'

    I did a search for "Select Case True" in my primary project's code and found 100s of cases. Admittedly, I did find a few where an If ... Then ... Else would have done the job, and a couple of others where I could have isolated a "test variable". But I see no problem with leaving it the way it is.

    Here's a fairly good example where I find a Select Case True ideal for the situation:

    Code:
    
        rsAchievedClone.Index = "PrimaryKey"
        rsAchievedClone.Seek "=", rsSurgeriesClone![MRN], TheDate
        Select Case True
        Case rsAchievedClone.NoMatch
            bAchievedRecordFound = False
        Case IsNull(rsAchievedClone![SurgeryDate])
            bAchievedRecordFound = False
        Case rsAchievedClone![SurgeryDate] <> rsSurgeriesClone![Date] ' TheDate is the encounter date.  We need to match the surgery date.
            bAchievedRecordFound = False
        Case Else
            bAchievedRecordFound = True
        End Select
    
    
    Notice that I can't do a complex boolean assignment, as the .NoMatch must be checked first. Therefore, I find the above approach ideal, as it gives me the short-circuiting that VB6 typically doesn't have.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

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