舉例介紹VB.NET類屬性詳細內(nèi)容
作者:佚名
初學者們可能正在苦于對VB.NET類屬性的相關概念無法很好的理解。不要緊,這篇文章就以幾段代碼示例來為大家詳細解讀其中含義。
也許還有很多人對于VB.NET這樣一款編程語言還不太了解。它是一款功能成強大的語言,直接面向?qū)ο?,并且可以?chuàng)造一個結(jié)構性比較牢固的編程化境。我們今天要為大家介紹的是VB.NET類屬性相關概念。其中有以下幾種需要舉例的。#t#
VB.NET類屬性1、
- Public Property Rank() As String
'注意這里的屬性名后面有個括號 - Get
- Return strPos
- End Get
- Set(ByVal value As String)
- strPos = value
- End Set
- End Property
VB.NET類屬性2、
- Public ReadOnly Property rHobby() As
String 'Readonly要在Property前面- Get
- Dim i As Integer
- Dim s As String
- s = Join(strHobby, ",")
'這個函數(shù)就是用來連接數(shù)組中的字符串的- Return s
- End Get
- End Property
VB.NET類屬性3、
- '這是定義索引器呀!
- Public ReadOnly Property indexHobby
(ByVal index As Integer) As String- Get
- If (strHobby Is Nothing) Or (index >
UBound(strHobby)) Then- '注意到上面的UBound()了沒?還有LBound()!
- '它們所在的命名空間是Microsoft.VisualBasic
- Return Nothing
- End If
- Return strHobby(index)
- End Get
- End Property
VB.NET類屬性4、
- Public WriteOnly Property wHobby()
As String- Set(ByVal value As String)
- If value Is Nothing Then
- If Not (strHobby Is Nothing) And
strHobby.GetLength(0) > 1 Then- ReDim Preserve strHobby(UBound
(strHobby) - 1)- End If
- Else
- If strHobby Is Nothing Then
- ReDim strHobby(0)
- Else
- ReDim Preserve strHobby(UBound
(strHobby) + 1)- End If
- strHobby(UBound(strHobby)) = value
- End If
- End Set
- End Property
5、
- Default Public Property Words
(ByVal index As Integer) As
String'注意Default- Get '注意到參數(shù)了嗎?使用這個屬性的時候,
就跟實現(xiàn)了索引器效果一樣。
<ClassObj(index)>- Words = theWords(index)
- End Get
- Set(ByVal value As String)
- theWords(index) = value
- End Set
- End Property
責任編輯:曹凱
來源:
博客園