0

我注意到在我必须阅读的代码库中,有时属性定义包含一个空的 ()。那是什么意思?它与数组无关。

例如 :

Public Property TotalPages() As Integer
4

2 回答 2

7

我知道这看起来很奇怪(对我们 C# 的人来说很好),但属性可以在 VB.NET 中具有参数。

所以你可以拥有

Public Class Student
    Private ReadOnly _scores(9) As Integer

    ' An indexed Score property
    Public Property Score(ByVal index As Integer) As _
        Integer
        Get
            Return _scores(index)
        End Get
        Set(ByVal value As Integer)
            _scores(index) = value
        End Set
    End Property

    Private _score As Integer

    ' A straightforward property
    Public Property Score() As _
        Integer
        Get
            Return _score
        End Get
        Set(ByVal value As Integer)
            _score = value
        End Set
    End Property

End Class

Public Class Test
    Public Sub Test()

        Dim s As New Student

        ' use an indexed property
        s.Score(1) = 1

        ' using a standard property   
        ' these two lines are equivalent
        s.Score() = 1
        s.Score = 1

    End Sub
End Class

所以你的声明

Public Property TotalPages() As Integer

是一个简单的非索引属性,例如没有参数。

于 2012-04-06T12:36:51.243 回答
5

它表明该属性不带任何参数:它不是索引属性。

索引属性具有一个或多个索引。这允许属性表现出类似数组的特性。例如,看下面的类:

  Class Class1  
   Private m_Names As String() = {"Ted", "Fred", "Jed"} 
    ' an indexed property. 
    Readonly Property Item(Index As Integer) As String 
     Get 
      Return m_Names(Index) 
     End Get 
   End Property  
End Class 

从客户端,您可以使用以下代码访问 Item 属性:

Dim obj As New Class1 
Dim s1 String s1 = obj.Item(0)

来自MSDN 杂志的索引属性说明

于 2012-04-06T12:37:39.390 回答