ZetCode

FreeBasic This Keyword

last modified June 23, 2025

The FreeBasic This keyword is a reference to the current object instance within type methods. It allows access to the object's members and helps distinguish between local and member variables.

Basic Definition

In FreeBasic, This is an implicit reference to the current object instance when working with user-defined types (UDTs). It's similar to this in C++ or self in Python.

The This keyword is primarily used within type methods to access the object's fields and methods. It helps resolve naming conflicts between parameters and member variables.

Accessing Member Variables

This example shows basic usage of This to access member variables.

this_member_access.bas
Type Person
    fname As String
    age As Integer

    Declare Sub SetInfo(fname As String, age As Integer)
    Declare Sub PrintInfo()
End Type

Sub Person.SetInfo(fname As String, age As Integer)
    This.fname = fname
    This.age = age
End Sub

Sub Person.PrintInfo()
    Print "Name: "; This.fname
    Print "Age: "; This.age
End Sub

Dim p As Person
p.SetInfo("John Doe", 35)
p.PrintInfo()

Here, This is used to distinguish between the parameter fname and the member variable fname. Without This, the assignment would be ambiguous. The PrintInfo method also uses This for clarity.

Returning Current Object

This can be used to return the current object from a method. This allows for method chaining, where multiple methods can be called on the same object in a single expression.

this_return_object.bas
Type Counter
    value As Integer
    
    Declare Function Increment() As Counter Ptr
    Declare Sub PrintValue()
End Type

Function Counter.Increment() As Counter Ptr
    This.value += 1
    Return @This
End Function

Sub Counter.PrintValue()
    Print "Value: "; This.value
End Sub

Dim c As Counter
c.Increment()->Increment()->PrintValue()

In this example, the Increment method modifies the value field of the Counter type and returns a pointer to the current object using This. The PrintValue method accesses the value field using This to print it to the console.

Passing objects

The This keyword can also be used to pass the current object as a parameter to other functions or methods. This is useful when you want to modify the object from an external function or when you need to pass the object to another method for further processing.

program.bas
Type Point
    x As Integer
    y As Integer

    Declare Sub Move(dx As Integer, dy As Integer)
    Declare Sub Show()
End Type

Sub Point.Move(dx As Integer, dy As Integer)
    This.x += dx
    This.y += dy
End Sub

Sub Point.Show()
    Print "Point: ("; This.x; ", "; This.y; ")"
End Sub


Sub Translate(ByRef p As Point, tx As Integer, ty As Integer)
    p.Move(tx, ty)
End Sub

Dim pt As Point
pt.x = 10
pt.y = 20
Translate(pt, 5, 5)
pt.Show()

This example demonstrates passing the current object pt to the Translate function. Inside Translate, the Move method is called on pt, which modifies its coordinates. The Show method then prints the updated coordinates of the point.

This in Constructor Methods

This can be used in constructor methods to initialize fields.

this_constructor.bas
Type Rectangle
    width As Integer
    height As Integer

    Declare Constructor(w As Integer, h As Integer)
    Declare Function Area() As Integer
End Type

Constructor Rectangle(w As Integer, h As Integer)
    This.width = w
    This.height = h
End Constructor

Function Rectangle.Area() As Integer
    Return This.width * This.height
End Function

Dim rect As Rectangle = Rectangle(10, 20)
Print "Area: "; rect.Area()

The constructor uses This to assign parameter values to member variables. This pattern ensures proper initialization of the object's state. The Area method then uses these values for calculation.

This tutorial covered the FreeBasic This keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.