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 ] name [ parameter_list ] [ return_type ]
[ Get = expression
| 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.
name
Mandatory name. For a default property:

Default-property name

[ Self ] ! name
Self
Optional keyword.
parameter_list
TODO
Not permitted in the one-line construct, or with an array property.
return_type
Optional clause. It can start with (1) As then a data type or (2) In then a unit of measure.
Get
Identifies the start of a block of code that returns the value of the property.
Mandatory if given the modifier @ReadOnly.
expression
Mandatory after «=», the value of the property. You cannot use statements with this construct. The next line must start with Set or End.
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, the compiler uses the type of the property.
statements
Optional 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, 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.

You can declare one or more default properties with a name that starts with «!» or «Self!» then an identifier. The identifier cannot have a type character. (See the paragraphs that follow for why this is not possible.) The properties must have different parameter signatures. You call a default property with parentheses after the name of the object.

For example, given two properties, one with one parameter and two with two parameters: You can call the property Self!one with obj(123). And you can call Self!two with obj("testing", 123).

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 name, without quotation marks (" ").

You can declare one or more properties with a name that starts with «!» or «Self!» then an identifier. To use a property for dictionary access, it must have only one parameter of type String.

The identifier can have a type character (%, !, @, or $) at the end. You call such a property with the given type character attached to the string identifier. For example, you can call a property Self!int% with obj!foo% or obj!bar%. For these examples, the equivalent calls are obj.int%("foo") and obj.int%("bar").

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!int%(key As String)
    Get
        ' Do something
    End Property
    @ReadOnly Property Self!str$(key As String)
    Get
        ' Do something
    End Property
End Class

New MyClass myObject

myObject.RunQuery

' Call properties as usual.
PrintLine myObject.int%("Items")
PrintLine myObject.str$("Name")

' Call properties with dictionary access.
PrintLine myObject!Items%
PrintLine myObject!Name$

See also