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)

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...