English 中文(简体)
1. 创建与开关说明中扼制相比的班级
原标题:Creating a class that can be compared to a string in a switch statement
  • 时间:2012-01-13 01:09:41
  •  标签:
  • vb.net

能否在VB中设立一个班级。 能够与开关说明中的插图相比较的网络? 例如,我要说的是“oo”类:

Public Class Foo
    Public Bar As String = "test"
End Class

能否实施某些接口或压倒某些平等操作者,以便我能够像现在这样使用Foo?

Dim foo As New Foo()

Select Case "test"
    Case foo
          It worked!
End Select
问题回答

Yes, you can define implicit conversion operators in the .NET languages that allow the compiler to implicitly convert an instance of your class to another type.

在VB。 NET称为“开发”经营者。 请将此定义如下:

Public Class Foo
    Public Bar As String = "test"

    Public Shared Widening Operator CType(ByVal f As Foo) As String
        Return f.Bar
    End Operator
End Class

There is also explicit conversion, which is called the "Narrowing" operator in VB.NET. Just as it sounds, the former conversion can happen automatically, while the latter requires you to explicitly instruct the compiler to perform the conversion. This can prevent some nasty surprises, but also clutters the code.

我认为,根据你的问题,你想看到某个行业的律师协会是否与考试相等。

如果是的话,你可以发挥杠杆作用:

Public Class Foo
    Public Bar As String = "test"

    Public Overrides Function ToString() As String
        Return Bar
    End Function
End Class

您的案情陈述成为:

    Dim foo As New Foo()

    Select Case "test"
        Case foo.ToString
              It worked!
    End Select

You could also implement a default property, but that isn t as clean because a default property are required to have a parameter.

Public Class Foo
    Public Bar As String = "test"

    Default Public ReadOnly Property DefaultProp(JustUseZero As Integer) As String
        Get
            Return Bar
        End Get
    End Property
End Class

which would then be called as:

    Dim foo As New Foo()

    Select Case "test"
        Case foo(0)
              It worked!
    End Select




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签