[This post is part of a series, "wish-list for future versions of VB"]

 

VB already has a powerful "Select Cast" statement, e.g.

        Select Case x

            Case Is = 1

                Console.WriteLine("x is equal to 1")

            Case Is > 7

                Console.WriteLine("x is greater than 7")

            Case 2 To 4

                Console.WriteLine("x is between 2 and 4")

        End Select

 

IDEA: Allow Select Case on object identity. For example,

        Select Case sender

            Case Is TextBox1

                Console.WriteLine("sender was TextBox1")

            Case Is TextBox2

                Console.WriteLine("sender was TextBox2")

        End Select

 

IDEA: Allow Select Case on type. For example,

        Select Case TypeOf(sender)

            Case Is TextBox

                ' Type of "sender" is TextBox inside this block:

                sender.ReadOnly = True

            Case Is Label

                ' Type of "sender" is Label inside this block:

                sender.AutoEllipsis = True

        End Select

This would provide an alternative to the common object-oriented style of writing a "visitor" pattern by overriding the "Visit" method on each subtype. Instead, in the manner of F# and other functional languages, you could gather the visiting code together in one place. Note that you wouldn't be allowed to GOTO from one branch to another (except maybe from a subtype to a supertype!)

 

Provisional evaluation from VB team: This is a decent idea, worth considering against the other decent ideas. (If you have other scenarios, please write in with them!)