ZetCode

FreeBasic With/End With Keywords

last modified June 16, 2025

The FreeBasic With/End With construct provides a shorthand way to access multiple members of an object or type. It reduces repetitive code when working with complex structures.

Basic Definition

The With statement in FreeBasic allows you to specify an object reference once, then access multiple members without repeating the object name. The block ends with End With.

This construct improves code readability and can make programs slightly more efficient by reducing repeated object lookups. It's particularly useful when working with structures or objects that have many members.

Basic With/End With Usage

This example demonstrates the simplest use of With/End With with a structure.

basic_with.bas
Type Point
    x As Integer
    y As Integer
End Type

Dim p As Point

With p
    .x = 10
    .y = 20
End With

Print "Point coordinates: "; p.x; ","; p.y

Here we create a Point structure and use With to set its x and y members. Instead of writing p.x and p.y, we use .x and .y inside the With block. The dot prefix refers to the p object.

With Block for Multiple Operations

With blocks are especially useful when performing several operations on an object.

multiple_operations.bas
Type Rectangle
    x As Integer
    y As Integer
    width As Integer
    height As Integer
End Type

Dim rect As Rectangle

With rect
    .x = 5
    .y = 5
    .width = 100
    .height = 50
    Print "Area: "; .width * .height
End With

This example shows how With can clean up code when working with objects that require multiple property assignments. We set four properties and calculate the area, all without repeating the rect variable name.

Nested With Statements

FreeBasic allows nesting With statements to work with hierarchical structures.

nested_with.bas
Type Address
    street As String
    city As String
End Type

Type Person
    name As String
    home As Address
End Type

Dim person As Person

With person
    .name = "John Doe"
    With .home
        .street = "123 Main St"
        .city = "Springfield"
    End With
End With

Print person.name; " lives at "; person.home.street; ", "; person.home.city

Here we nest With blocks to access properties at different levels. The outer With handles the Person object, while the inner With works with its Address sub-object. This makes hierarchical data access cleaner.

With and Arrays

With blocks can be used with array elements for concise array manipulation.

with_array.bas
Type Player
    name As String
    score As Integer
End Type

Dim players(1 To 3) As Player

With players(2)
    .name = "Alice"
    .score = 850
End With

Print "Player 2: "; players(2).name; " - Score: "; players(2).score

This example demonstrates using With with an array element. We access the second player in the array and set its properties without repeating the array index. The code remains clear even with array indexing.

With and Function Calls

With blocks can contain function calls that operate on the current object.

with_functions.bas
Type Circle
    x As Integer
    y As Integer
    radius As Integer
End Type

Function Area(c As Circle) As Double
    Return 3.14159 * c.radius * c.radius
End Function

Dim circ As Circle

With circ
    .x = 50
    .y = 50
    .radius = 10
    Print "Circle area: "; Area(circ)
End With

Here we use a With block to set up a Circle object and then call a function that operates on it. The function call appears within the With block, though it still needs the full object reference as a parameter.

With and Method Chaining

With blocks can make method chaining more readable in object-oriented code.

method_chaining.bas
Type StringBuilder
    buffer As String
    
    Declare Sub Append(s As String)
    Declare Sub Clear()
    Declare Function ToString() As String
End Type

Sub StringBuilder.Append(s As String)
    This.buffer += s
End Sub

Sub StringBuilder.Clear()
    This.buffer = ""
End Sub

Function StringBuilder.ToString() As String
    Return This.buffer
End Function

Dim sb As StringBuilder

With sb
    .Clear()
    .Append("Hello")
    .Append(" ")
    .Append("World")
    Print .ToString()
End With

This example shows how With can improve readability when calling multiple methods on an object. Instead of prefixing each method with sb, we use the With block to establish the context once.

With and Complex Expressions

With blocks can contain complex expressions involving the current object.

complex_expressions.bas
Type Vector
    x As Double
    y As Double
    
    Declare Function Magnitude() As Double
End Type

Function Vector.Magnitude() As Double
    Return Sqr(This.x * This.x + This.y * This.y)
End Function

Dim v As Vector

With v
    .x = 3.0
    .y = 4.0
    Print "Magnitude: "; .Magnitude()
    Print "Normalized x: "; .x / .Magnitude()
    Print "Normalized y: "; .y / .Magnitude()
End With

Here we use a With block to perform multiple calculations involving a Vector object. The block contains method calls and mathematical operations, all referencing the vector's properties with the shorthand dot notation.

Best Practices

This tutorial covered the FreeBasic With/End With construct 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.