Revision [21870]

This is an old revision of KeyPgProperty made by fxm on 2017-08-23 16:55:48.

 

PROPERTY


Declares or defines a property in a type or class

Syntax:
{ Type | Class } typename
declare Property fieldname () [ byref ] as datatype
declare Property fieldname ( [ byref | byval ] new_value as datatype )
declare Property fieldname ( [ byref | byval ] index as datatype ) [ byref ] as datatype
declare Property fieldname ( [ byref | byval ] index as datatype, [ byref | byval ] new_value as datatype )
End { Type | Class }

Property typename.fieldname () [ byref ] as datatype [ Export ]
statements
End Property

Property typename.fieldname ( [ byref | byval ] new_value as datatype ) [ Export ]
statements
End Property

Property typename.fieldname ( [ byref | byval ] index as datatype ) [ byref ] as datatype [ Export ]
statements
End Property

Property typename.fieldname ( [ byref | byval ] index as datatype, [ byref | byval ] new_value as datatype ) [ Export ]
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 fieldname(Index) = Value.
A get-Property can also return a reference by specifying byref as return_type.

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.

Note: A standard Property (get & set) does not work with combination operators (as "+="). But a result byref get-Property (as more generally any result byref function) works with combination operators.

Examples:
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:
Back to User Defined Types
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode