ZetCode

FreeBasic Shared Keyword

last modified June 16, 2025

The FreeBasic Shared keyword allows variables to be accessed across different scopes within a module. It bridges the gap between global and local variable scoping.

Basic Definition

In FreeBasic, Shared declares variables that are visible within a module but not globally. They persist between function calls while avoiding global namespace pollution.

Shared variables are declared at module level but can be accessed by all procedures within that module. They provide controlled access compared to global variables.

Simple Shared Variable

This example demonstrates basic usage of a shared variable.

shared_basic.bas
Dim Shared counter As Integer = 0

Sub IncrementCounter()
    counter += 1
    Print "Counter: "; counter
End Sub

IncrementCounter()
IncrementCounter()
IncrementCounter()

Here we declare a shared counter variable. The IncrementCounter subroutine can access and modify it. The variable maintains its value between calls, acting like a module-level static variable.

Shared vs Global Variables

This example compares shared and global variable behavior.

shared_vs_global.bas
Dim Shared moduleVar As String = "Module"
Dim globalVar As String = "Global"

Sub ShowVars()
    Print "Module var: "; moduleVar
    Print "Global var: "; globalVar
End Sub

ShowVars()

Both variables are accessible in the subroutine, but moduleVar is limited to the module. The global variable can be accessed by any code in the program, while the shared one stays module-private.

Shared in Multiple Procedures

Shared variables can be used across multiple procedures in a module.

shared_multiple.bas
Dim Shared userStatus As String

Sub SetStatus(newStatus As String)
    userStatus = newStatus
End Sub

Sub PrintStatus()
    Print "Current status: "; userStatus
End Sub

SetStatus("Active")
PrintStatus()
SetStatus("Inactive")
PrintStatus()

The userStatus variable is shared between two procedures. SetStatus modifies it, while PrintStatus reads it. This demonstrates shared state management within a module.

Shared Arrays

Arrays can also be declared as shared for module-wide access.

shared_array.bas
Dim Shared values(1 To 5) As Integer

Sub FillArray()
    For i As Integer = 1 To 5
        values(i) = i * 10
    Next
End Sub

Sub PrintArray()
    For i As Integer = 1 To 5
        Print values(i); " ";
    Next
    Print
End Sub

FillArray()
PrintArray()

The shared array values is accessible to both procedures. FillArray populates it, and PrintArray displays the contents. Shared arrays are useful for module-level data storage.

Shared with Static Variables

This example combines shared and static variables.

shared_static.bas
Dim Shared total As Integer = 0

Function AddToTotal(value As Integer) As Integer
    Static calls As Integer = 0
    calls += 1
    total += value
    Print "Call "; calls; ": Added "; value; ", Total: "; total
    Return total
End Function

AddToTotal(10)
AddToTotal(20)
AddToTotal(30)

The shared total accumulates values across function calls, while the static calls tracks invocation count locally. This shows how shared and static variables can work together.

Shared in Nested Scopes

Shared variables remain accessible even in nested scopes.

shared_nested.bas
Dim Shared outerVar As String = "Outer"

Sub OuterSub()
    Print "Outer: "; outerVar
    
    Sub InnerSub()
        Print "Inner: "; outerVar
        outerVar = "Modified"
    End Sub
    
    InnerSub()
    Print "Outer after inner: "; outerVar
End Sub

OuterSub()

The shared outerVar is accessible in both the outer and inner subroutines. Changes made in the inner scope persist in the outer scope, demonstrating shared variable visibility across nested scopes.

Shared with User-Defined Types

User-defined types can be declared as shared for module-wide use.

shared_udt.bas
Type Person
    name As String
    age As Integer
End Type

Dim Shared user As Person

Sub SetPerson(n As String, a As Integer)
    user.name = n
    user.age = a
End Sub

Sub PrintPerson()
    Print "Name: "; user.name; ", Age: "; user.age
End Sub

SetPerson("Alice", 30)
PrintPerson()

The shared Person variable user is accessible to both procedures. One sets the values, while the other displays them. This pattern is common for module-level configuration or state.

Best Practices

This tutorial covered the FreeBasic Shared 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.