Skip to content
FutureBlitz

    Install

    Your own types, 3 of 8

    Properties

    Give a type members that read like fields but run code, to check values and work them out.

    What you’ll build

    A Thermostat that holds a temperature in Celsius, shows it in Fahrenheit too, and keeps every setting within a safe range. Code that uses it reads and sets thermostat.Celsius as if it were a field, while the Thermostat checks each value it’s given.

    A Property with a Get

    A Property is a member that’s read like a field, but whose value comes from code: its Get. Get => value gives one expression; a Get block of several lines ends with End Get. A Property with only a Get can be read, and not assigned.

    thermostat.fb
    Import std.core
    
    Class Thermostat
        Field celsius: Float = 20.0
    
        Property Fahrenheit: Float
            Get => celsius * 9.0 / 5.0 + 32.0
        End Property
    
        Property Comfortable: Bool
            Get
                Return celsius >= 18.0 && celsius <= 24.0
            End Get
        End Property
    End Class
    
    Local thermostat = Thermostat()
    Print(thermostat.Fahrenheit, thermostat.Comfortable)
    Output
    68 True

    Assigning thermostat.Comfortable = False is an error, since Comfortable has no Set.

    A Set that checks

    A Set runs when the Property is assigned, and its parameter is the new value. It can check the value, change it, or store it somewhere else. Here Celsius keeps the setting between 5 and 30 degrees, and Fahrenheit converts and passes its value on to Celsius.

    thermostat.fb
    Import std.core
    Import std.math
    
    Class Thermostat
        Field celsius: Float = 20.0
    
        Property Celsius: Float
            Get => celsius
            Set(value)
                celsius = Clamp(value, 5.0, 30.0)
            End Set
        End Property
    
        Property Fahrenheit: Float
            Get => celsius * 9.0 / 5.0 + 32.0
            Set(value)
                Celsius = (value - 32.0) * 5.0 / 9.0
            End Set
        End Property
    End Class
    
    Local thermostat = Thermostat()
    thermostat.Celsius = 40.0
    Print(thermostat.Celsius)
    thermostat.Fahrenheit = 68.0
    Print(thermostat.Celsius)
    thermostat.Celsius += 1.5
    Print(thermostat.Celsius, thermostat.Fahrenheit)
    Output
    30
    20
    21.5 70.7

    thermostat.Celsius += 1.5 calls the Get, adds, and calls the Set with the sum. Clamp, from std.math, keeps a value between two others.

    Static Properties

    A Static Property belongs to the type, as a Static Field does, and is read through the type’s name. Its Get has no object to use.

    thermostat.fb
    Import std.core
    
    Class Thermostat
        Static Field installed: Int
    
        New()
            installed++
        End New
    
        Static Property Installed: Int
            Get => installed
        End Property
    End Class
    
    Local rooms = [Thermostat(), Thermostat(), Thermostat()]
    Print("{Thermostat.Installed} thermostats for {rooms.Count} rooms")
    Output
    3 thermostats for 3 rooms

    Properties in a Struct

    A Struct can have Properties too. Its Get reads the fields, and its Set can change them, like a ByRef Method, so a Struct’s Property can only be assigned on a variable.

    size.fb
    Import std.core
    
    Struct Size
        Field Width: Int
        Field Height: Int
    
        Property Area: Int
            Get => Width * Height
        End Property
    
        Property Square: Int
            Get => Width
            Set(side)
                Width = side
                Height = side
            End Set
        End Property
    End Struct
    
    Local box = Size(3, 4)
    Print(box.Area)
    box.Square = 5
    Print(box.Width, box.Height, box.Area)
    Output
    12
    5 5 25

    What you learned

    • A Property reads like a field, and its value comes from its Get.
    • A Set runs on assignment and can check or convert the value; without one, the Property can’t be assigned.
    • += and ++ on a Property call its Get, then its Set.
    • A Static Property belongs to the type, and a Struct’s Set changes the Struct.

    Report an issue with this page or with FutureBlitz on GitHub.