42

I'm not sure if this valid C#, but hopefully you get the idea. :)

switch (msg.GetType()) {
    case ClassA:
        // blah
    case ClassB:
        // blah 2
    case ClassC:
        // blah 3
}

How would I switch on an object's type but using VB.NET's Select Case?

I'm aware that some might suggest using polymorphism, but I'm using a hierarchy of small message classes so that really wouldn't work in my case.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mcjabberz
  • 9,788
  • 10
  • 36
  • 38

5 Answers5

81

With VB 2010, for projects targeting .NET framework 4 and later, you can now do this:

Select Case msg.GetType()
    Case GetType(ClassA)
End Select

In earlier VB versions, it didn't work because you couldn't compare two types with equality. You'd have to check if they point to the same reference using the Is keyword. It's not possible to do this in a Select Case, unless you use a property of the type like the Name or FullName for comparison, as suggested by Michael. You can use a combination of If and ElseIf though:

Dim type = msg.GetType()
If type Is GetType(ClassA)
    ...
ElseIf type Is GetType(ClassB)
    ...
...
End If
Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
  • I'm just going to use `If`/`ElseIf`s. Kinda sucks that there's not a switch-like way to do it. – mcjabberz Aug 24 '09 at 18:19
  • 1
    To be specific, this will only work in .Net Framework 4 and later. [Type.Equality Operator](http://msdn.microsoft.com/en-us/library/system.type.op_equality.aspx) – Jon Jul 30 '12 at 14:43
  • 3
    You can take this with a grain of salt and do your own perf testing: My own simple performance test of `If/ElseIf` vs `Select Case` proved the `If/ElseIf` option was faster every time. The `Select Case` took roughly 4 times as long. I'll stick with `If/ElseIf` for now. – Airn5475 Jan 14 '13 at 16:09
  • Thanks - this allows for more succinct / easier-to-read code when checking for 2 different types in same comparison eg `Case GetType(ClassA), GetType(ClassB)` vs `Elseif typeof obj is ClassA or typeof obj is ClassB then` – user3085342 Sep 22 '17 at 02:07
24

Well, if you insist on using Select Case, you could always go with:

Select Case True
    Case TypeOf msg Is ClassA
        ' do something '
    Case TypeOf msg Is ClassB
        ' do something else '
    Case Else
        ' and so on '
End Select

But I would imagine most people like to avoid this kind of thing. If/ElseIf would probably be clearer.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • 11
    Please do avoid this sort of thing! :) – MarkJ Aug 20 '09 at 16:42
  • 2
    I am ambivalent on this one if the block is short. What would be the argument against this section of code? – lb. Nov 25 '13 at 00:37
  • 6
    Actually this does not seem bad at all, especially when you might have multiple cases (fall-through) - this would look MILES better than if statements... – Mladen Mihajlovic Feb 12 '14 at 14:23
  • 3
    I prefer CASE over nested IFs - much more readable and less error prone IMO. I use SELECT CASE True ... often, works great. – rheitzman Oct 15 '15 at 21:17
9

This is a way to handle Button1 and Button2 click events in the same sub (I started out as a VB6 programmer, so this is a good substitute for VB6 handling of control arrays)

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
                Select Case True
                    Case sender Is Me.Button1
                        ' Do Button1 stuff '
                    Case sender Is Me.Button2
                        ' Do Button2 stuff '
                End Select
            End Sub
Dan
  • 91
  • 1
  • 1
6

I wouldn't ever select case true, but you can do this:

Select Case msg.GetType.Name
    Case GetType(ClassA).Name
        ...
    Case GetType(ClassB).Name
        ...
    Case Else
        ...
End Select

Which is slighly cleaner looking than this:

If msg.GetType Is GetType(ClassA) Then
    ...
ElseIf msg.GetType Is GetType(ClassB) Then
    ...
Else
    ...
End If
Mr Shoubs
  • 14,629
  • 17
  • 68
  • 107
  • It may be more sightly, but is also about a hundred times slower too. But it would get the job done if he really wants to do it that way. – Bernesto Sep 24 '11 at 03:23
  • @Bernesto - A hundred times slower than what? This answer is the same as the accepted answer (I don't have 2010 to test the performance of type equality). I don't believe it should be a -1?? – Mr Shoubs Sep 27 '11 at 12:11
  • If Type.Name is not a fully qualified type name there may be a conflict if two classes have the same name but are in different namespaces. – lb. Nov 25 '13 at 00:35
  • 1
    @MrShoubs "A hundred times slower than what?" You do realize that, unlike the accepted answer, which just compares Type instances, your answer creates strings for each of them and compares those, instead? – Tom Lint Mar 13 '18 at 14:00
1

This:

Dim a As Object = New TextBox

Select Case True
    Case TypeOf a Is TextBox
        MsgBox("aaa")

    Case TypeOf a Is ComboBox

    Case TypeOf a Is ListBox

End Select
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fredou
  • 19,848
  • 10
  • 58
  • 113