<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.harlamenkov.ru/index.php?action=history&amp;feed=atom&amp;title=EN%2Fdocumentation%2Fbasic_guide%2Fscope_of_variables</id>
	<title>EN/documentation/basic guide/scope of variables - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.harlamenkov.ru/index.php?action=history&amp;feed=atom&amp;title=EN%2Fdocumentation%2Fbasic_guide%2Fscope_of_variables"/>
	<link rel="alternate" type="text/html" href="https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/scope_of_variables&amp;action=history"/>
	<updated>2026-11-11T13:51:18Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/scope_of_variables&amp;diff=15884&amp;oldid=prev</id>
		<title>Sancho: Created page with &quot;&lt;section begin=title /&gt;&lt;noinclude&gt;{{DISPLAYTITLE:&lt;/noinclude&gt;Scope and Life Span of Variables&lt;noinclude&gt;}}&lt;/noinclude&gt;&lt;section end=title /&gt;{{BreadCrumbL10n}}   {{Documentation/BA...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/scope_of_variables&amp;diff=15884&amp;oldid=prev"/>
		<updated>2011-09-01T14:18:36Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;section begin=title /&amp;gt;&amp;lt;noinclude&amp;gt;{{DISPLAYTITLE:&amp;lt;/noinclude&amp;gt;Scope and Life Span of Variables&amp;lt;noinclude&amp;gt;}}&amp;lt;/noinclude&amp;gt;&amp;lt;section end=title /&amp;gt;{{BreadCrumbL10n}}   {{Documentation/BA...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;section begin=title /&amp;gt;&amp;lt;noinclude&amp;gt;{{DISPLAYTITLE:&amp;lt;/noinclude&amp;gt;Scope and Life Span of Variables&amp;lt;noinclude&amp;gt;}}&amp;lt;/noinclude&amp;gt;&amp;lt;section end=title /&amp;gt;{{BreadCrumbL10n}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Documentation/BASICGuideTOC&lt;br /&gt;
|ShowPrevNext=block&lt;br /&gt;
|ShowPrevPage=block&lt;br /&gt;
|PrevPage=EN/documentation/basic_guide/arrays&lt;br /&gt;
|NextPage=EN/documentation/basic_guide/constants&lt;br /&gt;
|lang=block&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
__NOTOC__ &lt;br /&gt;
A variable in {{OOo}} Basic has a limited life span and a limited scope from which it can be read and used in other program fragments. The amount of time that a variable is retained, as well as where it can be accessed from, depends on its specified location and type.&lt;br /&gt;
&lt;br /&gt;
== Local Variables ==&lt;br /&gt;
&lt;br /&gt;
Variables that are declared in a function or a procedure are called local variables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Test&lt;br /&gt;
  Dim MyInteger As Integer&lt;br /&gt;
  ' ...&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Local variables only remain valid as long as the function or the procedure is executing, and then are reset to zero. Each time the function is called, the values generated previously are not available.&lt;br /&gt;
&lt;br /&gt;
To keep the previous values, you must define the variable as &amp;lt;tt&amp;gt;Static&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Test&lt;br /&gt;
  Static MyInteger As Integer&lt;br /&gt;
  ' ...&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{VBAnote|Unlike VBA, {{OOo}} Basic ensures that the name of a local variable is not used simultaneously as a global and a private variable in the module header. When you port a VBA application to {{OOo}} Basic, you must change any duplicate variable names.}}&lt;br /&gt;
&lt;br /&gt;
== Public Domain Variables ==&lt;br /&gt;
&lt;br /&gt;
Public domain variables are defined in the header section of a module by the keyword &amp;lt;tt&amp;gt;Dim&amp;lt;/tt&amp;gt;. These variables are available to all of the modules in their library:&lt;br /&gt;
&lt;br /&gt;
Module A:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Dim A As Integer&lt;br /&gt;
Sub Test&lt;br /&gt;
  Flip&lt;br /&gt;
  Flop&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
Sub Flip&lt;br /&gt;
 A = A + 1&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Module B:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Flop&lt;br /&gt;
  A = A - 1&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The value of variable &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; is not changed by the &amp;lt;tt&amp;gt;Test&amp;lt;/tt&amp;gt; function, but is increased by one in the &amp;lt;tt&amp;gt;Flip&amp;lt;/tt&amp;gt; function and decreased by one in the &amp;lt;tt&amp;gt;Flop&amp;lt;/tt&amp;gt; function. Both of these changes to the variable are global.&lt;br /&gt;
&lt;br /&gt;
You can also use the keyword &amp;lt;tt&amp;gt;Public&amp;lt;/tt&amp;gt; instead of &amp;lt;tt&amp;gt;Dim&amp;lt;/tt&amp;gt; to declare a public domain variable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Public A As Integer&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A public domain variable is only available so long as the associated macro is executing and then the variable is reset.&lt;br /&gt;
&lt;br /&gt;
== Global Variables ==&lt;br /&gt;
&lt;br /&gt;
In terms of their function, global variables are similar to public domain variables, except that their values are retained even after the associated macro has executed. Global variables are declared in the header section of a module using the keyword &amp;lt;tt&amp;gt;Global&amp;lt;/tt&amp;gt;: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Global A As Integer&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Private Variables ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;Private&amp;lt;/tt&amp;gt; variables are only available in the module in which they are defined. Use the keyword &amp;lt;tt&amp;gt;Private&amp;lt;/tt&amp;gt; to define the variable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Private MyInteger As Integer&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If several modules contain a &amp;lt;tt&amp;gt;Private&amp;lt;/tt&amp;gt; variable with the same name, {{OOo}} Basic creates a different variable for each occurrence of the name. In the following example, both module &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;B&amp;lt;/tt&amp;gt; have a &amp;lt;tt&amp;gt;Private&amp;lt;/tt&amp;gt; variable called &amp;lt;tt&amp;gt;C&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;Test&amp;lt;/tt&amp;gt; function first sets the &amp;lt;tt&amp;gt;Private&amp;lt;/tt&amp;gt; variable in module &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; and then the &amp;lt;tt&amp;gt;Private&amp;lt;/tt&amp;gt; variable in module &amp;lt;tt&amp;gt;B&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Module A:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Private C As Integer&lt;br /&gt;
&lt;br /&gt;
Sub Test&lt;br /&gt;
  SetModuleA      ' Sets the variable C from module A&lt;br /&gt;
  SetModuleB      ' Sets the variable C from module B&lt;br /&gt;
  ShowVarA        ' Shows the variable C from module A (= 10)&lt;br /&gt;
  ShowVarB        ' Shows the variable C from module B (= 20)&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
Sub SetModuleA&lt;br /&gt;
  C = 10&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
Sub ShowVarA&lt;br /&gt;
  MsgBox C        ' Shows the variable C from module A. &lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Module B:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt; &lt;br /&gt;
Private C As Integer&lt;br /&gt;
 &lt;br /&gt;
Sub SetModuleB&lt;br /&gt;
  C = 20&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
Sub ShowVarB&lt;br /&gt;
  MsgBox C        ' Shows the variable C from module B.&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Keep in mind that ShowVarB only shows the expected value of C (20) because Sub Test is keeping it in scope. If the calls to SetModuleB and ShowVarB are independent, e.g. SetModuleB is triggered from one toolbar button and ShowVarB is triggered from another toolbar button, then ShowVarB will display a C value of 0 since module variables are reset after each macro completion.&lt;br /&gt;
 &lt;br /&gt;
{{PDL1}}&lt;/div&gt;</summary>
		<author><name>Sancho</name></author>
		
	</entry>
</feed>