我注意到在代碼庫中我必須閱讀,有時屬性定義包含一個空()。那是什麼意思?它與數組無關。()在屬性定義中代表什麼?
例如:
Public Property TotalPages() As Integer
我注意到在代碼庫中我必須閱讀,有時屬性定義包含一個空()。那是什麼意思?它與數組無關。()在屬性定義中代表什麼?
例如:
Public Property TotalPages() As Integer
我知道這似乎很奇怪(當然我們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
聲明是一個簡單的非索引屬性,例如沒有參數。
它顯示該屬性不採用任何參數:它不是索引屬性。
索引屬性有一個或多個索引。這允許一個屬性展現類似數組的特質。例如,看一下下面的類:
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
從客戶端,你可以用下面的代碼訪問項目屬性:
Dim obj As New Class1
Dim s1 String s1 = obj.Item(0)
索引屬性的解釋從MSDN magazine
()上屬性表示一個數組。在這個特定的實例中,它聲明瞭一個整數數組。在VB中有趣的花絮,Public Property TotalPages()as Integer與Public Property TotalPages相同爲Integer() – 2012-04-06 14:51:18
@RobertBeaubien:不,它沒有聲明一個整數數組,除非你把括號放在最後。 – 2012-04-06 19:52:28