在 VB.NET 中选择对象类型的大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1301881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:21:18  来源:igfitidea点击:

Select Case on an object's type in VB.NET

vb.netswitch-statementselect-case

提问by mcjabberz

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

我不确定这个 C# 是否有效,但希望你能明白。:)

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?

我将如何打开对象的类型但使用 VB.NET 的类型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.

我知道有些人可能会建议使用polymorphism,但我使用的是小消息类的层次结构,因此在我的情况下确实不起作用。

回答by Meta-Knight

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

使用 VB 2010,对于面向 .NET Framework 4 及更高版本的项目,您现在可以执行以下操作:

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:

在早期的 VB 版本中,它不起作用,因为您无法相等地比较两种类型。您必须使用 Is 关键字检查它们是否指向相同的引用。在 Select Case 中不可能这样做,除非您使用 Name 或 FullName 等类型的属性进行比较,如 Michael 所建议的。您可以使用 If 和 ElseIf 的组合:

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

回答by Dan Tao

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

好吧,如果您坚持使用 Select Case,您可以随时使用:

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.

但我想大多数人都喜欢避免这种事情。If/ElseIf 可能会更清楚。

回答by Dan

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)

这是一种在同一个子程序中处理 Button1 和 Button2 单击事件的方法(我最初是一名 VB6 程序员,所以这是 VB6 处理控件数组的一个很好的替代品)

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

回答by Mr Shoubs

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

我永远不会select case true,但你可以这样做:

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

回答by Fredou

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