Property
 
声明或定义类型或类中的属性

语法

{ Type | Class } typename
Declare Property fieldname () As datatype
Declare Property fieldname ( [ ByRef | ByVal ] new_value As datatype )
Declare Property fieldname ( [ ByRef | ByVal ] index As datatype ) As datatype
Declare Property fieldname ( [ ByRef | ByVal ] index As datatype, [ ByRef | ByVal ] new_value As datatype )
End { Type | Class }

Property typename .fieldname () As datatype
statements
End Property

Property typename .fieldname ( [ ByRef | ByVal ] new_value As datatype )
statements
End Property

Property typename .fieldname ( [ ByRef | ByVal ] index As datatype ) As datatype
statements
End Property

Property typename .fieldname ( [ ByRef | ByVal ] index As datatype, [ ByRef | ByVal ] new_value As datatype )
statements
End Property

参数

typename
TypeClass的名称
fieldname
物业名称
new_value
传递给要赋值的财产的值
index
物业指数值

说明

Property字段用于以与其他数据字段相同的方式获取和设置TypeClass的值,而不是对字段的简单赋值或从字段检索的值执行过程。

typename 是声明和定义Property方法的类型的名称。typename 的名称解析遵循与Namespace中使用的过程相同的规则。

A 属性可以可选地具有一个索引参数。索引时,以Property(Index)= Value的形式访问属性。

typename 相同类型的隐藏This参数传递给属性过程。This用于访问TypeClass的字段。

例子

Type Vector2D
  As Single x, y
  Declare Operator Cast() As String
  Declare Property Length() As Single
  Declare Property Length( ByVal new_length As Single )
End Type

Operator Vector2D.cast () As String
  Return "(" + Str(x) + "," + Str(y) + ")"
End Operator

Property Vector2D.Length() As Single
  Length = Sqr( x * x + y * y )
End Property

Property Vector2D.Length( ByVal new_length As Single )
  Dim m As Single = Length
  If m <> 0 Then
    ''new vector = old / length * new_length
    x *= new_length / m
    y *= new_length / m
  End If
End Property

Dim a As Vector2D = ( 3, 4 )

Print "a ="; a
Print "a.length ="; a.length
Print

a.length = 10

Print "a ="; a
Print "a.length ="; a.length

输出:
a = (3, 4)
a.length =  5

a = (6, 8)
a.length =  10
属性索引:
  ''True/False
Namespace BOOL
  Const FALSE = 0
  Const TRUE = Not FALSE
End Namespace

Type BitNum
  Num As UInteger
  
    ''使用索引获取/设置每个属性。
  Declare Property NumBit( ByVal Index As Integer ) As Integer
  Declare Property NumBit( ByVal Index As Integer, ByVal Value As Byte )
End Type

  '' Get a bit by it's指数。
Property BitNum.NumBit( ByVal Index As Integer ) As Integer
  Return Bit( This.Num, Index )
End Property

  '' Set a bit by it's指数。
Property BitNum.NumBit( ByVal Index As Integer, ByVal Value As Byte )

    ''确保索引为整数范围。
  If Index >= ( SizeOf(This.Num) * 8 ) Then
    Print "离开uInteger范围!"
    Exit Property
  Else
    If Index < 0 Then Exit Property
  End If
  
  If Value = BOOL.FALSE Then
    This.Num = BitReset( This.Num, Index )
  End If
  
  If Value = BOOL.TRUE Then
    This.Num = BitSet( This.Num, Index )
  End If
  
End Property


Dim As BitNum Foo


Print "使用数据类型测试属性索引:"
Print "FOO数值:" & Foo.Num

  ''将数字中的位设置为true。
Foo.NumBit(31) = BOOL.TRUE
Print "设置FOO的第31位"

  ''打印看看我们的位是否已经更改。
Print "FOO数值:" & Foo.Num
Print "FOO第31位?" & Foo.NumBit(31)
Sleep
Print ""

输出:
Testing property indexing with data types:
FOO Number's Value: 0
Set the 31st bit of FOO
FOO Number's Value: 2147483648
FOO 31st Bit Set? -1
参考