举例介绍VB.NET类属性详细内容

也许还有很多人对于VB.NET这样一款编程语言还不太了解。它是一款功能成强大的语言,直接面向对象,并且可以创造一个结构性比较牢固的编程化境。我们今天要为大家介绍的是VB.NET类属性相关概念。其中有以下几种需要举例的。#t#

VB.NET类属性1、

 

  1. Public Property Rank() As String 
    '注意这里的属性名后面有个括号  
  2. Get  
  3. Return strPos  
  4. End Get  
  5. Set(ByVal value As String)  
  6. strPos = value 
  7. End Set  
  8. End Property 

 

VB.NET类属性2、

 
 
 
  1. Public ReadOnly Property rHobby() As 
    String 'Readonly要在Property前面  
  2. Get  
  3. Dim i As Integer  
  4. Dim s As String  
  5. s = Join(strHobby, ",") 
    '这个函数就是用来连接数组中的字符串的  
  6. Return s  
  7. End Get  
  8. End Property 

 

VB.NET类属性3、

 
 
 
  1. '这是定义索引器呀!  
  2. Public ReadOnly Property indexHobby
    (ByVal index As Integer) As String  
  3. Get  
  4. If (strHobby Is Nothing) Or (index > 
    UBound(strHobby)) Then  
  5. '注意到上面的UBound()了没?还有LBound()!  
  6. '它们所在的命名空间是Microsoft.VisualBasic  
  7. Return Nothing  
  8. End If  
  9. Return strHobby(index)  
  10. End Get  
  11. End Property 

VB.NET类属性4、

 

 
 
 
  1. Public WriteOnly Property wHobby() 
    As String  
  2. Set(ByVal value As String)  
  3. If value Is Nothing Then  
  4. If Not (strHobby Is Nothing) And 
    strHobby.GetLength(0) 
    > 1 Then  
  5. ReDim Preserve strHobby(UBound
    (strHobby) - 1)  
  6. End If  
  7. Else  
  8. If strHobby Is Nothing Then  
  9. ReDim strHobby(0)  
  10. Else  
  11. ReDim Preserve strHobby(UBound
    (strHobby) + 1)  
  12. End If  
  13. strHobby(UBound(strHobby)) = value  
  14. End If  
  15. End Set  
  16. End Property 

 

 

5、

 

 
 
 
  1. Default Public Property Words
    (ByVal index As Integer) As 
    String'注意Default  
  2. Get '注意到参数了吗?使用这个属性的时候,
    就跟实现了索引器效果一样。
    <ClassObj(index)> 
  3. Words = theWords(index)  
  4. End Get  
  5. Set(ByVal value As String)  
  6. theWords(index) = value  
  7. End Set  
  8. End Property 

 

THE END