VB.NET之类属性

有三种需要举例的

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

     


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


     
    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

    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 '注意到参数了吗?使用这个属性的时候,就跟实现了索引器效果一样。<ClassObj(index)>
                Words 
    =  theWords(index)
            
    End   Get
            
    Set ( ByVal  value  As   String )
                theWords(index) 
    =  value
            
    End   Set
        
    End Property

     

     

     

     

     

     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值