()在属性定义中代表什么?

时间:2012-04-06 12:32:24

标签: .net vb.net syntax properties

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

例如:

Public Property TotalPages() As Integer

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分: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 magazine

的索引属性说明