Difference between revisions of "Documentation/BASIC Guide/Constants"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(One intermediate revision by the same user not shown)
Line 10: Line 10:
 
Constants are values which may be used but not changed by the program.
 
Constants are values which may be used but not changed by the program.
 
== Defining Constants ==
 
== Defining Constants ==
In {{OOo}} Basic, use the keyword <tt>Const</tt> to declare a constant.
+
In {{AOo}} Basic, use the keyword <tt>Const</tt> to declare a constant.
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Const A = 10
 
Const A = 10
 
Const B = A+5
 
Const B = A+5
 
Const Hi = "Hello World"
 
Const Hi = "Hello World"
</source>
+
</syntaxhighlight>
  
 
{{Warn|Do not specify the constant type in the declaration : it will be silently ignored.
 
{{Warn|Do not specify the constant type in the declaration : it will be silently ignored.
  
 
This code shows that you do not get the type that you specify.
 
This code shows that you do not get the type that you specify.
  <source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Const alpha As Long = 1
 
Const alpha As Long = 1
 
Const beta As Single = 3.1
 
Const beta As Single = 3.1
Line 33: Line 33:
 
MsgBox(TypeName(delta)) ' displays : Double
 
MsgBox(TypeName(delta)) ' displays : Double
 
MsgBox(TypeName(phi))  ' displays : Double
 
MsgBox(TypeName(phi))  ' displays : Double
  </source>
+
</syntaxhighlight>
 
As Basic makes automatic type conversions, there is usually no problems using a constant in an expression.
 
As Basic makes automatic type conversions, there is usually no problems using a constant in an expression.
  
 
If you want to specify the type of a data, use a typed variable, not a constant.
 
If you want to specify the type of a data, use a typed variable, not a constant.
 
}}
 
}}
 
  
 
== Scope of Constants ==
 
== Scope of Constants ==
Line 44: Line 43:
 
Constants have the same scope as variables (see [[Documentation/BASIC Guide/Scope of Variables|Scope and Life Span of Variables]]), but the syntax is slightly different.  A <tt>Const</tt> definition in the module header is available to the code in that module.  To make the definition available to other modules, add the <tt>Public</tt> keyword.
 
Constants have the same scope as variables (see [[Documentation/BASIC Guide/Scope of Variables|Scope and Life Span of Variables]]), but the syntax is slightly different.  A <tt>Const</tt> definition in the module header is available to the code in that module.  To make the definition available to other modules, add the <tt>Public</tt> keyword.
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Public Const one = 1
 
Public Const one = 1
</source>
+
</syntaxhighlight>
 
== Predefined Constants ==
 
== Predefined Constants ==
  
{{PRODUCTNAME}} Basic predefines several constants. Among the most useful are:
+
{{AOo}} Basic predefines several constants. Among the most useful are:
 
*<tt>True</tt> and <tt>False</tt>, for Boolean assignment statements
 
*<tt>True</tt> and <tt>False</tt>, for Boolean assignment statements
 
*<tt>PI</tt> as a type <tt>Double</tt> numeric value
 
*<tt>PI</tt> as a type <tt>Double</tt> numeric value
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim bHit as Boolean
 
Dim bHit as Boolean
 
bHit = True
 
bHit = True
Line 60: Line 59:
 
' ... (assign a value to dRadius)
 
' ... (assign a value to dRadius)
 
dArea = PI * dRadius * dRadius
 
dArea = PI * dRadius * dRadius
</source>
+
</syntaxhighlight>
 
   
 
   
 
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Constants}}
 
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Constants}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 11:09, 30 January 2021


Constants are values which may be used but not changed by the program.

Defining Constants

In Apache OpenOffice Basic, use the keyword Const to declare a constant.

Const A = 10
Const B = A+5
Const Hi = "Hello World"
Documentation caution.png Do not specify the constant type in the declaration : it will be silently ignored.

This code shows that you do not get the type that you specify.

Const alpha As Long = 1
Const beta As Single = 3.1
Const gamma As Boolean = True
Const delta As Currency = 123456.05
Const phi As Long = 32768
 
MsgBox(TypeName(alpha)) ' displays : Integer
MsgBox(TypeName(beta))  ' displays : Double
MsgBox(TypeName(gamma)) ' displays : Integer
MsgBox(TypeName(delta)) ' displays : Double
MsgBox(TypeName(phi))   ' displays : Double

As Basic makes automatic type conversions, there is usually no problems using a constant in an expression.

If you want to specify the type of a data, use a typed variable, not a constant.

Scope of Constants

Constants have the same scope as variables (see Scope and Life Span of Variables), but the syntax is slightly different. A Const definition in the module header is available to the code in that module. To make the definition available to other modules, add the Public keyword.

Public Const one = 1

Predefined Constants

Apache OpenOffice Basic predefines several constants. Among the most useful are:

  • True and False, for Boolean assignment statements
  • PI as a type Double numeric value
Dim bHit as Boolean
bHit = True
 
Dim dArea as Double, dRadius as Double
' ... (assign a value to dRadius)
dArea = PI * dRadius * dRadius


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools