Skip to content

SHARED

Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgShared Last revised: 2021-09-29

Variable declaration modifier specifying visibility throughout a module.

Syntax

Dim Shared ...
Redim Shared ...
Common Shared ...
Static Shared ...
[Static] Var Shared ...

Description

Shared makes module-level variables visible inside Subs and Functions. If Shared is not used on a module-level variable's declaration, the variable is only visible to the module-level code in that file.

Notes (for Shared variables excluding Common variables):

  • Generally a Shared variable may only be initialized with a constant value (its starting value is set at the start of the program in the .data section before any code is run).
  • A first exception is a Shared variable of var-len string type, that never can be initialized, even with a constant string.
  • A second exception is a Shared variable of user-defined type having a constructor even implicit, that can be initialized with a non-constant value.

To access from a local scope block to duplicated symbols of Shared variables defined in the global namespace, add one or preferably two dot(s) as prefix: .SomeSymbol or preferably ..SomeSymbol.

Examples

vb
' Compile with -lang qb or fblite
'$lang: "qb"

Declare Sub MySub

Dim Shared x As Integer
Dim y As Integer

x = 10
y = 5

MySub

Sub MySub
    Print "x is "; x 'this will report 10 as it is shared
    Print "y is "; y 'this will not report 5 because it is not shared
End Sub

Differences from QB

  • The Shared statement inside scope blocks (functions, subs, if/thens, and loops) is not supported. Use Dim|Redim|Common|Static Shared in the main program instead.

See Also

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft