Results 1 to 19 of 19

Thread: [RESOLVED] Select Case Statement Question

Hybrid View

  1. #1
    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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

  5. #5
    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.

  6. #6
    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...

  7. #7
    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.

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