Skip to content

PROPERTY


Declares or defines a property in a type or class

Syntax

vb
{ 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 Property without index parameter must always be used without empty parentheses after fieldname.

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.

Note: When a get-Property is defined with one index parameter, the fieldname= syntax can not be used to return a value. For such a get-Property, the property= syntax (in addition to the return syntax) is only the one allowed.

Examples

start GeSHi

vb
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

end GeSHi

Output:

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

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

Property Indexing:

start GeSHi

vb
  '' 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 ""

end GeSHi

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

Back to DocToc

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft