I suggest you ...

Using TypeOf operator in the Select Case statement

Select TypeOf myobj
....Case SByte, Short, Integer, Long

End Select

55 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Robert EinhornRobert Einhorn shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    CharlesCharles shared a merged idea: Redimension of variable type within scope  ·   · 

    Hey everyone,

    If you hadn’t heard already last week at BUILD we announced the PUBLIC release of a Preview of the next version of the Visual Basic language and code editing experience powered by “Roslyn”. You can download this preview and install it on top of Visual Studio 2013 today!

    I’m excited to let you know that included in this is a preview of a new language feature we’re considering that let’s you Select Case on the type of an object like this:

    Select Case obj
    Case b As Button

    Case t As TextBox

    End Case

    You can read about it more here: http://www.codeplex.com/Download?ProjectName=roslyn&DownloadId=824695

    Download the End User Preview @ http://msdn.microsoft.com/en-US/vstudio/roslyn

    I encourage you all to download it, try it out, and send us your feedback.

    Regards,

    Anthony D. Green, Program Manager, Visual Basic & C# Languages Team

    9 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Craig JohnsonCraig Johnson commented  ·   ·  Flag as inappropriate

        Tried it out in Roslyn preview from //Build. Love it. It has the feel of the F# Match expression, very powerful. I like the When conditionals as well.

      • Anthony D. Green [MSFT]Anthony D. Green [MSFT] commented  ·   ·  Flag as inappropriate

        Hey Charles,

        If you hadn't heard already last week at BUILD we announced the PUBLIC release of a Preview of the next version of the Visual Basic language and code editing experience powered by "Roslyn". You can download this preview and install it on top of Visual Studio 2013 today!

        I'm excited to let you know that included in this is a preview of a new language feature we're considering: Select Case on type. It gets *really* close to what you're asking for - it just requires using a different name (the individual cases can have the same name, they just can't be the same as a variable or parameter that's already been declared):

        Public Function DoStuff(V As Object) As String
        Select Case V
        Case V2 As String
        Return V2
        Case V2 As Double
        Return V2.ToString("0.0")
        Case V2 As String() When V2.Length > 0
        Return V2(0)
        Case Else
        Return ""
        End Select
        End Function

        You can read about it more here: http://www.codeplex.com/Download?ProjectName=roslyn&DownloadId=824695

        Download the End User Preview @ http://msdn.microsoft.com/en-US/vstudio/roslyn

        I encourage you to download it, try it out, and send us your feedback.

        Regards,

        Anthony D. Green, Program Manager, Visual Basic & C# Languages Team

      • Eric LameyEric Lamey commented  ·   ·  Flag as inappropriate

        Add linq/lambda as well

        Select case From c as customer in myList(of Customer) where c.contains("Marsha").First
        Case "Greg"
        Case "Marsha"
        Case "Peter"
        End Select

        The possibilities are limitless especially you allow "Case" to have some logic.

        Select case From c as customer in myList(of object) select c
        Case is typeOf Dictionary(of string,string)
        Case is typeOf ListOf(String)
        Case is typeOf Dictionary(of Date, String)
        End Select

      • Robert EinhornRobert Einhorn commented  ·   ·  Flag as inappropriate

        I have to say my automatic strong typed feature would not be safe sometimes but your "Catch style" syntax would be safe.
        The following situation would cause an unexpected run-time error instead of a compiling error:
        State 1 with a single type condition without problem:

        Case CheckBox
            It.Checked = True

        State 2 with a new type condition and a forgot assignment with the Checked property but there would not be compiling error unfortunately because of the late bound:

        Case CheckBox, TextBox
            It.Checked = True ' chance of an unexpected run-time error

        Same with your "Catch style" syntax.
        State 1 with a single type condition without problem of course:

        Case chk As CheckBox
            chk.Checked = True

        State 2 with a new type condition and a forgot assignment also with the Checked property:
        (provided that you would like to also implement a type-list feature with the Case statement)
        In this code, there would be a safe compiling error: 'chk' is not declared

        Case CheckBox, TextBox
            chk.Checked = True

        ----------------------------------------------

        Nevertheless in my opinion, the It-reference would be steel useful but without automatic strong typed feature.
        In other words, the type of the It-reference would be always same as the type of the testexpression.

      • Robert EinhornRobert Einhorn commented  ·   ·  Flag as inappropriate

        @Anthony
        The strong-typed reference and the When filter ideas are very good and useful.

        A strong-typed reference example based on your syntax:

        Select Case sender
            Case btn As Button
                btn.Text = "Ok"
        End Select

        Because of the declaration, I think, it is not clear that the selection is a type selection.

        In my opinion, the solution would be a reference for the testexpression in the Select block.
        I have two kind of syntax for the strong-typed reference idea.
        1.
        The following syntax would have a new reserved It-named keyword what would cause a breaking change unfortunately however it would be the ideal syntax:

        Select TypeOf sender
            Case Button
                It.Text = "Ok"
        End Select

        2.
        And a similar idea without breaking change:

        Select TypeOf sender Alias It
            Case Button
                It.Text = "Ok"
        End Select

        If the type of the testexpression (sender) is an object and
        there is only one type name after the Case keyword then
        the It-testexpression-reference would be a strong-typed reference automatically by the single type name.

        The TypeOf keyword would sign to the compiler that the default and hidden operator would be the TypeOf...Is operator in the background between the testexpression and the Case-type-item(s).

        The Select-TypeOf statement would be optimized for type checking but value and object equality checking also would be used with the It-reference.
        The AndAlso, Xor logical operators would be used in a Case-item but exclusively with the It-reference.
        The When filter also would be supported.

        In both concepts, the It-keyword would be a reference of the current testexpression.
        In both concepts, the It-keyword would substitute the Is-keyword in the current Select block.
        In both concepts, the It-reference would be used only inside its own Select block but not in a nested Select block.
        In both concepts, the using of the It-reference would be same.

        The Alias extension would be an optional declaration.
        After the Alias, the It-keyword would be a fixed named variable (not a reserved keyword).
        Actually, the optional ... Alias It ... would be a temporary compromise without breaking change.
        In the future, the ... Alias It ... and the Is-keyword would be removed from the
        Visual Basic to use the more ideal It-reserved-keyword (1. idea).

        advantages:
        - redundancy-free type checking
        - clear syntax
        - regular Visual Basic syntax
        - automatic strong-typed reference
        - more effective development with the It-reference
        - reusable code with the It-reference
        - AndAlso, Xor logical operators can be used in a Case-item
        - When filter can be used
        - easy to learn and use (... Alias It ... by intellisenese)
        - easy to read
        - etc...

        Some examples:

        - the ... Alias It ... would be optional:

        Select TypeOf sender
            Case Button, TextBox, Label

        End Select

        - an other strong-typed reference example:

        Select TypeOf sender Alias It
            Case Button
                It.Text = "Ok"
                ' ** early bound (intellisenese is working) **
            Case Label, TextBox
                It.Text = ""
                ' ** late bound (intellisenese is not working) **
        End Select

        - the following code would cause a compiling error at the "Alias It":
        Local variable 'It' is already declared

        Dim It As String
        Select TypeOf sender Alias It

        End Select

        - the It-reference would be used only inside its own Select block
        so, the following code would cause a compiling error at the "It.Text" assignment:
        'It' is not declared

        Select TypeOf sender Alias It
            Case Button
                Select TypeOf obj1
                    Case Class1
                        It.Text = "Ok"
                End Select
        End Select

        - an example with the When filter:

        Select TypeOf sender
            Case TextBox, Label, ListBox When blnEnabled

            Case CheckBox, RadioButton

            Case Button
                It.Text = "Ok"
        End Select

        Without the When filter it should redesign the previous code to an If structure what would always cause a very redundant code.

        Even, I can also imagine the next with the using of the It-reference in a Case-item.
        It would be very interesting and useful sometimes but perhaps it would reduce the easy code reading compared to the present Select structure.
        However, a similar If structure often would be even worse in this context.

        - AndAlso operator using with a property of the It-reference:
        (this would not be solved with the When filter)

        Select TypeOf sender Alias It
            Case Label, TypeOf It Is Button AndAlso It.Enabled

            Case TextBox

        End Select

        - type/value/object-equality checks in the same Select-TypeOf block:

        Select TypeOf objNumber Alias It
            Case Byte
                MsgBox("type checking")
            Case It = 500
                MsgBox("value checking")
            Case It Is objSum
                MsgBox("object equality checking")
        End Select

        - and an other:

        Select TypeOf objNumber Alias It
            Case Byte, It = 500, It Is objSum

        End Select

      • Robert EinhornRobert Einhorn commented  ·   ·  Flag as inappropriate

        @Guillermo
        The most important feature of a Select structure is the redundancy-free selection.
        Your code is redundant.
        The True/False checking is practically equivalent to an If structure what would be also redundant.
        In other cases, some redundant OrElse operator can be avoided but there is no other benefit.

      • GuillermoGuillermo commented  ·   ·  Flag as inappropriate

        I been using this trick for ages without problems:

        Select Case True

        Case TypeOf ob Is Button

        Case TypeOf ob Is TextBox

        End Select

      • Robert EinhornRobert Einhorn commented  ·   ·  Flag as inappropriate

        Hey Anthony,

        Thanks for the status update.
        It's a very good news.
        I'm sorry that I did not answer yet but there are a lot of question:
        What syntax would be compatible syntax?
        What syntax would be the best without breaking change?
        What syntax would be the best with breaking change?
        What syntax would be the most familiar Visual Basic syntax?
        What syntax would be the most most regular Visual Basic syntax?
        What syntax would be the most comfortable syntax?
        What syntax would be the most readable syntax?
        What syntax would be the most learnable syntax?
        What syntax would be the most usable syntax?
        What syntax would be the most effective syntax?
        What syntax would be the most appropriate for the future?
        for example:
        - real testexpression reference in the Select-Case block
        - AndAlso operator in a Case item

        What syntax would be the best based on the frequently used operations?
        One common syntax or (and) three kind of syntax based on the checkings?
        - value checking: Select Case
        - type checking: Select TypeOf
        - object equality checking: Select Object

        There are many aspects and I want to give a good answer.
        I'm thinking about it...
        some days...

        Regards,
        Robert

      Feedback and Knowledge Base