Difference between revisions of "Memory Variables"
Yvonnemilne (Talk | contribs) |
Yvonnemilne (Talk | contribs) |
||
Line 1: | Line 1: | ||
==Naming== | ==Naming== | ||
− | Memory variable names must begin with a letter (A-Z, a-z) or an underscore (-), followed by any combination of letters, digits or underscores. The variable name can be of any length, but only the first | + | Memory variable names must begin with a letter (A-Z, a-z) or an underscore (-), followed by any combination of letters, digits or underscores. The variable name can be of any length, but only the first 32 characters are significant, so these must be unique. The Recital/4GL ignores the case of letters, so m_var, M_VAR, and m_VaR would all be treated as the same memory variable name. The name given to a memory variable has no bearing on the type of data that is, or can be, stored in it. In fact, the type of data stored in a particular variable can be changed at any time, although this needs to be carefully controlled so that inappropriate operations are not attempted. e.g. |
<code lang="recital"> | <code lang="recital"> | ||
m_var = 1234 | m_var = 1234 | ||
− | m_var = | + | m_var = 'a character value' |
? m_var + 100 | ? m_var + 100 | ||
</code> | </code> | ||
Line 13: | Line 13: | ||
<code lang="recital"> | <code lang="recital"> | ||
− | store | + | store 'new value' to cVAR1 |
− | cVAR1 = | + | cVAR1 = 'new value' |
</code> | </code> | ||
Line 20: | Line 20: | ||
<code lang="recital"> | <code lang="recital"> | ||
− | store | + | store 'new value' to cVAR1, cVAR2 |
</code> | </code> | ||
Latest revision as of 09:45, 22 October 2009
Contents
Naming
Memory variable names must begin with a letter (A-Z, a-z) or an underscore (-), followed by any combination of letters, digits or underscores. The variable name can be of any length, but only the first 32 characters are significant, so these must be unique. The Recital/4GL ignores the case of letters, so m_var, M_VAR, and m_VaR would all be treated as the same memory variable name. The name given to a memory variable has no bearing on the type of data that is, or can be, stored in it. In fact, the type of data stored in a particular variable can be changed at any time, although this needs to be carefully controlled so that inappropriate operations are not attempted. e.g.
m_var = 1234 m_var = 'a character value' ? m_var + 100
Assignment
Values are assigned to memory variables using the STORE command or the equals = operator.
store 'new value' to cVAR1 cVAR1 = 'new value'
Note that the STORE command can assign a value to more than one memory variable in a single command.
store 'new value' to cVAR1, cVAR2
Declaration and Visibility
Values can be assigned to memory variables without those memory variables having been pre-declared, but it is generally accepted that the pre-declaration of memory variables is good programming practice.
Variables can be declared as PUBLIC, PRIVATE or LOCAL and will be initialized as a logical false (.F.).
public cVar1 private cVar2 local cVar3
Public
Public variables are globally visible: they are accessible and can be changed from any part of an application. If the application is run from the Interactive Prompt, then any public variables can still be accessed even after the application ends. Any variables created at the Interactive Prompt are automatically created as public variables.
Private
Private variables are only visible within the declaring module (program, procedure, User Defined Function) and any modules called by that declaring module. Any variables accessed within a module that are not pre-declared are automatically created as private variables. When the module returns, then all of the memory variables and arrays that were declared by the PRIVATE command are released.
Local
Local variables are only visible within the declaring module and are released when the module returns. LOCAL variables differ from PRIVATE variables in that a LOCAL variable is not visible to lower level procedures or functions.
Arrays
The Recital/4GL supports the use of both one-dimensional and two-dimensional static arrays. In almost all cases, arrays must be pre-declared before reference can be made to them.
Declaration can be made using the DECLARE / DIMENSION commands or the PUBLIC / PRIVATE / LOCAL commands. The size of the array can be specified in parentheses or square brackets.
private aArr1[10] public aArr2(10) declare aArr3[10,9]
Values can be assigned to a single element by specifying its element number, or to all the elements in an array.
// Assign value to all elements aArr1 = [hello] // Assign value to one element aArr1[1] = [hello] aArr3[2,4] = [hello]
Arrays are always passed to procedures or functions by REFERENCE i.e. any changes made in the called program will be reflected in the actual array.
Dynamic Arrays
The Recital/4GL also supports the definition of dynamic arrays - arrays to which additional elements can be added. To declare a dynamic array, simply omit the number of elements.
private aDynarray[]
Elements are added using arrayname.element syntax.
aDynarray.name = [Recital Corporation] aDynarray.email = [info@recital.com]
They can then be referenced by element number or by element name.
? aDynarray.name Recital Corporation ? aDynarray[2] info@recital.com