VB.NET overload()运算符

前端之家收集整理的这篇文章主要介绍了VB.NET overload()运算符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是VB.NET的新手,并且搜索一个复制DataRow行为的方法.
在VB.NET中我可以写这样的东西:
Dim table As New DataTable
'assume the table gets initialized
table.Rows(0)("a value") = "another value"

现在如何用括号访问班级成员?我以为我可以重载()运算符,但这似乎不是答案.

它不是一个过载运算符,这被称为 default property.

“A class,structure,or interface can designate at most one of its properties as the default property,provided that property takes at least one parameter. If code makes a reference to a class or structure without specifying a member,Visual Basic resolves that reference to the default property.” – MSDN –

DataRowCollection类和DataRow类都有一个名为Item的默认属性.

|       |
table.Rows.Item(0).Item("a value") = "another value"

这允许您在不指定Item成员的情况下编写代码

table.Rows(0)("a value") = "another value"

这是一个带有默认属性自定义类的简单示例:

Public Class Foo

    Default Public Property Test(index As Integer) As String
        Get
            Return Me.items(index)
        End Get
        Set(value As String)
            Me.items(index) = value
        End Set
    End Property

    Private ReadOnly items As String() = New String(2) {"a","b","c"}

End Class
Dim f As New Foo()
Dim a As String = f(0)

f(0) = "A"

鉴于上面的示例,您可以使用字符串类的默认属性获取指定位置的字符.

f(0) = "abc"
Dim c As Char = f(0)(1) '<- "b" | f.Test(0).Chars(1)

猜你在找的VB相关文章