Property
Declares or defines a property in a type or class
Syntax
`
{ 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
`
Parameters
`typename`
name of the `Type` or `Class`
`fieldname`
name of the property
`new_value`
the value passed to property to be assigned
`index`
the property index value
Description
`Property` fields are used to get and set values of a `Type` or `Class` in the same way as other data fields except instead of a simple assignment to a field or a value retrieved from field, a procedure is executed.
`typename` is the name of the type for which the `Property` method is declared and defined. Name resolution for `typename` follows the same rules as procedures when used in a `Namespace`.
A Property may optionally have one index parameter. When indexed, properties are accessed as Property(Index) = Value.
A hidden `This` parameter having the same type as `typename` is passed to the property procedure. `This` is used to access the fields of the `Type` or `Class`.
Example
`
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
`
Output:

    a = (3, 4)
    a.length =  5

    a = (6, 8)
    a.length =  10

Property Indexing:
`
'' True/False
Namespace BOOL
Const FALSE = 0
Const TRUE = Not FALSE
End Namespace
Type BitNum
Num As UInteger
'' Get/Set Properties each with an Index.
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 index.
Property BitNum.NumBit( ByVal Index As Integer ) As Integer
Return Bit( This.Num, Index )
End Property
'' Set a bit by it's index.
Property BitNum.NumBit( ByVal Index As Integer, ByVal Value As Byte )
'' Make sure index is in Integer range.
If Index >= ( SizeOf(This.Num) * 8 ) Then
Print "Out of uInteger Range!"
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 "Testing property indexing with data types:"
Print "FOO Number's Value: " & Foo.Num
'' Set the bit in the number as true.
Foo.NumBit(31) = BOOL.TRUE
Print "Set the 31st bit of FOO"
'' Print to see if our bit has been changed.
Print "FOO Number's Value: " & Foo.Num
Print "FOO 31st Bit Set? " & Foo.NumBit(31)
Sleep
Print ""
`
Output:

    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

See also

- `Class`
- `Type`