Property Statement

Declares the names of properties and the procedures used to change and return values.

Syntax

One-line construct

[ modifier ] _
Property [ Dim | Var ] declarator [ , declarator ]… _
[ Does trait_name . supplied_name ]

Block construct

[ modifier ] _
Property [ Dim | Var ] declarator [ parameter_list ]
[ Get
    [ statements ] ]
[ Set parameter
    [ statements] ]
End [ Property ]

Parts

modifier
Optional
  • @Abstract – The property has no implementation. You must implement it in a child class. See @Abstract.
  • @Backed – The property automatically reserves memory. The one-line construct and all constructs with Dim or Var are always backed.
  • @Deprecated – You can remove the property in a subsequent release of a library. See @Deprecated.
  • @Iterator – The property implements the iterator pattern. It is automatically read-only. See @Iterator.
  • @Open – The property can have a different implementation in a child class. See @open.
  • @Override – The property is a different implementation of a property inherited from a parent class. See @Override
  • @ReadOnly – The property is read-only. It must return the value with Get. It cannot change the value with Set.
  • @Shared – The property is a member of a class, and not an instance of that class. See @Shared.
  • @WriteOnly – The property is write-only. It must change the value with Set. It cannot return the value with Get.
You can put @Abstract together with @ReadOnly or @WriteOnly. You can put @Backed and @Deprecated together with all other modifiers.
Dim or Var
Usually optional keyword.
Mandatory if you specify the size of an array property with a name declared with Const. The compiler cannot be sure without one of these keywords.
declarator
Mandatory one or more declarations that have almost the same syntax as the statement Dim. The one-line construct lets you write many declarators with a comma between each.

Declarator

name [ ( [ dimensions ] ) ] [ As type | In unit ] [ = initializer ]
name
Mandatory name.
You can also make a property that you can call without its name. You give it a name that starts with Self. See default properties below.
dimensions
Optional one or more integer expressions with a comma between each. Not permitted with unbacked properties.
type
Optional data type
unit
Optional unit of measure
initializer
Optional expression with a data type compatible with type or unit.
parameter_list
TODO
Not permitted in the one-line construct, or with an array property.
Get
Identifies the start of a block of code that returns the value of the property.
Mandatory if given the modifier @ReadOnly.
Set
Identifies the start of a block of code that changes the value of the property.
Mandatory if given the modifier @WriteOnly.
parameter
Mandatory after Set. It must be the same data type as the property. If you do not specify a data type, then the compiler uses the type of the property.
statements
Optional executable statements
End
Completes the block construct. You can also use End Property.
You can change this part of the syntax. See @Option Directive for more information.

Instructions

A property has many of the same qualities as a method. A property has public visibility. You can declare a property in libraries, classes, and traits.

A property can have one of two constructs: a one-line construct or a block construct.

One-line construct

The one-line construct is always backed. Thus it automatically allocates memory with each instance of its owner.

Block construct

The block construct is usually not backed. Thus it does not automatically allocate memory with each instance of its owner. If necessary, then use the modifier @Backed.

default properties

A default property is a property procedure that you can call without a name. You can call such properties with only the name of an object and some arguments. This can decrease the code that you must write. But we do not recommend this because it can cause problems for other programmers who read your code.

Alternatively, you can use a default property to implement a dictionary access. This uses the operator «!» between the name of an object and a string. You must write the string as a correct name, without quotation marks (" "). You give one or more properties a name that starts with Self and has one parameter of type String.

There are five (5) different names that you can use:

Self
This is the most usual default property. It usually has no limits on types or number of parameters.
Self%
You use this property to return an integer type from a dictionary access. An example is «MyObject!Items%». The property declaration must have only one parameter of the type String. The argument passed is the name after «!» and without «%» ("Items" in this example).
Self!
You use this property to return a floating-point type from a dictionary access. An example is «MyObject!Capacity!». The property declaration must have only one parameter of the type String. The argument passed is the name between the two «!» ("Capacity" in this example).
Self@
You use this property to return the fixed-point type Fixed64 from a dictionary access. An example is «MyObject!Balance@». The property declaration must have only one parameter of the type String. The argument passed is the name after «!» and without «@» ("Balance" in this example).
Self$
You use this property to return a string from a dictionary access. An example is «MyObject!Name$». The property declaration must have only one parameter of the type String. The argument passed is the name after «!» and without «$» ("Name" in this example).

All properties that implement dictionary access must only return a value with Get. You can make sure with the modifier @ReadOnly.

Examples

One-line property

' Declare a Person class.
Class Person
    Property Name As String
End Class

' Make a Person object.
Person JD

' Assign it a value.
JD.Name = "John Doe"

' Show its value.
PrintLine JD.Name

One-line array property

Class Person
    Const MaxAddresses = 2
    Property Dim Address(MaxAddresses) As String
End Class

Block property, backed

Class Person
    @Backed Property Name As String
    Get
        ' do something
    Set value
        ' do something
    End Property
End Class

' do something

Block property, not backed

Class Person
    Property Name As String
    Get
        ' do something
    Set value
        ' do something
    End Property
End Class

' do something

default properties

Class MyClass
    Method RunQuery
        ' Do something
    End Method
    @ReadOnly Property Self%(key As String)
    Get
        ' Do something
    End Property
    @ReadOnly Property Self$(key As String)
    Get
        ' Do something
    End Property
End Class

New MyClass MyObject
MyObject.RunQuery
PrintLine MyObject!Items%
PrintLine MyObject!Name$

See also