views:

2220

answers:

5

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

+8  A: 

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

Edit: Actually, I'm wrong, it doesn't work... The reason why it doesn't work is that you can't compare two types with equality. You have to check if they point to the same reference using the Is keyword. I don't think it's 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.

In my own code, I did this instead:

Dim type = msg.GetType()
If type Is GetType(ClassA)
    ...
ElseIf type Is GetType(ClassB)
    ...
...
End If

It's not a select case but it does the same thing.

Meta-Knight
I'm just going to use `If`/`ElseIf`s. Kinda sucks that there's not a switch-like way to do it.
mcjabberz
A: 

Select Case must switch based on a constant expression, so unless you want to use If / ElseIf ... you'd have to do something like this

    Select Case msg.GetType().Name
        Case "Button"
            MsgBox("It's a button")
        Case Else
            MsgBox("Not a button")
    End Select

You could also use the FullName property to fully qualify the type.

Michael McCloskey
No, `Select Case` doesn't have to switch based on a constant expression.
Pavel Minaev
I stand corrected for VB.NET. Perhaps I should stay in my C# world. In any case, the expression must evaluate to one of the standard primitive data types.
Michael McCloskey
+3  A: 

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
Please do avoid this sort of thing! :)
MarkJ
A: 

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
Fredou
A: 

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