Stenway Developer Network

Properties

Auto-Implemented Properties

Auto-implemented properties are by default readonly (only getter)

package MyLib

class MyClass
  myProperty float
  
  constructor()
    this.myProperty = 10.0
  end
end

With value:

package MyLib

class MyClass
  myProperty1 float = 10
  myProperty2 bool = true
end

With setter:

package MyLib

class MyClass
  myProperty1 float set = 10
  myProperty2 float set
end

With access limited setter:

package MyLib

class MyClass
  myProperty1 float protected set
  myProperty2 float protected set = 12.3
end

Properties

A property with a manually provided backing property:

package MyLib

class MyClass
  private _myBackingProperty float set
  
  myProperty float
    get
      return this._myBackingProperty
    end
    set
      this._myBackingProperty = value
    end
  end
end

Shortened:

package MyLib

class MyClass
  private _myBackingProperty float set
  
  myProperty float
    get : return this._myBackingProperty
    set : this._myBackingProperty = value
  end
end

With a value check

package MyLib

using System

class MyClass
  private _myBackingProperty float set
  
  myProperty float
    get : return this._myBackingProperty
    set
      if value > 100 : throw InvalidArgumentError()
      this._myBackingProperty = value
    end
  end
end

Readonly property:

package MyLib

class MyClass
  myProperty float
    get
      return 123
    end
  end
end

Shorter:

package MyLib

class MyClass
  myProperty float
    get : return 123
  end
end

Access Levels

Lesser access level for setter

package MyLib

class MyClass
  private _myBackingProperty float set
  
  protected myProperty float
    get : return this._myBackingProperty
    private set : this._myBackingProperty = value
  end
end

Static Properties

Note that an auto-implemented static property can only be of a primitive type or must be nullable for custom types.

package MyLib

class MyClass
  private static _myBackingProperty float set
  
  static myProperty float
    get : return MyClass._myBackingProperty
    set : MyClass._myBackingProperty = value
  end

  static readonly myReadonlyProperty float
    get : return 123
  end
end

Abstract Properties

package MyLib

abstract class MyAbstractClass
  abstract myProperty float set
  abstract myReadonlyProperty float
end

class MyClass
  private _value float set
  
  override myProperty float
    get : return this._value
    set : this._value = value
  end
  
  override myReadonlyProperty float
    get : return 123
  end
end