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.
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.
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.
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.
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.
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.
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.
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
- Scope: Use shared for module-level variables that don't need global visibility.
- Initialization: Always initialize shared variables explicitly.
- Naming: Prefix shared variables to distinguish them from locals.
- Thread Safety: Be cautious with shared variables in multithreaded code.
- Encapsulation: Prefer procedures over direct shared variable access.
This tutorial covered the FreeBasic Shared keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.