FreeBasic Property Keyword
last modified June 16, 2025
The FreeBasic Property keyword is used to create property
accessors for class member variables. Properties provide controlled
access to class data through getter and setter methods.
Basic Definition
In FreeBasic, Property defines special methods that control
access to class fields. Properties can have get accessors (read), set
accessors (write), or both.
Properties enable encapsulation by hiding implementation details while providing controlled access. They can validate data, compute values, or trigger actions when accessed.
Simple Read-Only Property
This example demonstrates a basic read-only property with only a getter.
Type Person
Private:
Dim As String fullName
Public:
Declare Property Name() As String
Declare Constructor(ByVal n As String)
End Type
Constructor Person(ByVal n As String)
fullName = n
End Constructor
Property Person.Name() As String
Return fullName
End Property
Dim p As Person = Person("John Smith")
Print "Name: "; p.Name
Here we define a Person class with a private fullName
field. The Name property provides read-only access to this
field. The constructor initializes the private field.
Read-Write Property
This example shows a property with both get and set accessors.
Type Rectangle
Private:
Dim As Integer width_, height_
Public:
Declare Property Width() As Integer
Declare Property Width(ByVal w As Integer)
Declare Property Height() As Integer
Declare Property Height(ByVal h As Integer)
End Type
Property Rectangle.Width() As Integer
Return width_
End Property
Property Rectangle.Width(ByVal w As Integer)
If w > 0 Then
width_ = w
End If
End Property
Property Rectangle.Height() As Integer
Return height_
End Property
Property Rectangle.Height(ByVal h As Integer)
If h > 0 Then
height_ = h
End If
End Property
Dim rect As Rectangle
rect.Width = 100
rect.Height = 50
Print "Area: "; rect.Width * rect.Height
The Rectangle class uses properties to control access to its
dimensions. The set accessors include validation to ensure only positive
values are accepted. This demonstrates data protection.
Computed Property
Properties can compute values rather than just return stored values.
Type Circle
Private:
Dim As Single radius
Public:
Declare Property Radius() As Single
Declare Property Radius(ByVal r As Single)
Declare Property Area() As Single
End Type
Property Circle.Radius() As Single
Return radius
End Property
Property Circle.Radius(ByVal r As Single)
radius = r
End Property
Property Circle.Area() As Single
Return 3.14159 * radius * radius
End Property
Dim c As Circle
c.Radius = 5.0
Print "Radius: "; c.Radius
Print "Area: "; c.Area
The Area property doesn't store a value but computes it from
the radius. This shows how properties can act like methods while maintaining
field-like syntax.
Indexed Property
Properties can accept parameters to create indexed properties.
Type StringArray
Private:
Dim As String arr(0 To 9)
Public:
Declare Property Item(ByVal index As Integer) As String
Declare Property Item(ByVal index As Integer, ByVal value As String)
End Type
Property StringArray.Item(ByVal index As Integer) As String
If index >= 0 And index <= 9 Then
Return arr(index)
End If
Return ""
End Property
Property StringArray.Item(ByVal index As Integer, ByVal value As String)
If index >= 0 And index <= 9 Then
arr(index) = value
End If
End Property
Dim sa As StringArray
sa.Item(2) = "FreeBasic"
sa.Item(5) = "Property"
Print sa.Item(2)
Print sa.Item(5)
This example creates an indexed property that acts like an array accessor. The property validates the index range before accessing the internal array. Indexed properties enable custom collection behaviors.
Read-Only Property with Side Effects
Properties can perform actions when accessed, beyond just getting/setting.
Type Counter
Private:
Dim As Integer count
Dim As Integer accessCount
Public:
Declare Sub Increment()
Declare Property Value() As Integer
End Type
Sub Counter.Increment()
count += 1
End Sub
Property Counter.Value() As Integer
accessCount += 1
Print "Access #"; accessCount
Return count
End Property
Dim c As Counter
c.Increment()
c.Increment()
Print "Current value: "; c.Value
Print "Current value: "; c.Value
The Value property tracks how many times it's accessed while
still returning the counter value. This demonstrates that properties can
have side effects, though this should be used judiciously.
Property with Different Access Levels
Properties can have different access levels for get and set operations.
Type Account
Private:
Dim As Single balance
Public:
Declare Property Balance() As Single
Declare Sub Deposit(ByVal amount As Single)
Declare Sub Withdraw(ByVal amount As Single)
End Type
Property Account.Balance() As Single
Return balance
End Property
Sub Account.Deposit(ByVal amount As Single)
If amount > 0 Then
balance += amount
End If
End Sub
Sub Account.Withdraw(ByVal amount As Single)
If amount > 0 And amount <= balance Then
balance -= amount
End If
End Sub
Dim acc As Account
acc.Deposit(1000.0)
acc.Withdraw(200.0)
Print "Balance: $"; acc.Balance
Here the Balance property is read-only publicly, while deposit
and withdrawal are handled through methods. This pattern is common for
financial data where direct modification should be restricted.
Static Property
Properties can be static, operating on class-level rather than instance data.
Type Logger
Private:
Static As Integer instanceCount
Public:
Declare Static Property Count() As Integer
Declare Constructor()
End Type
Constructor Logger()
instanceCount += 1
End Constructor
Static Property Logger.Count() As Integer
Return instanceCount
End Property
Dim As Logger log1, log2, log3
Print "Logger instances: "; Logger.Count
The static Count property tracks how many Logger
instances exist. Static properties are shared across all instances of a class
and can be accessed without an instance.
Best Practices
- Encapsulation: Use properties to hide implementation details.
- Validation: Perform validation in property setters.
- Consistency: Keep property behavior predictable.
- Performance: Avoid expensive operations in properties.
- Naming: Use clear, descriptive names for properties.
- Side Effects: Minimize side effects in property accessors.
This tutorial covered the FreeBasic Property keyword with practical
examples showing various property patterns and use cases.
Author
List all FreeBasic Tutorials.