Variables
and Data Types
Variable Names [AX 2012]
The
name, or identifier, for a variable points to a memory location where
information of a specific data type is stored. The syntax rules for variable
names are as follows:
·
Names must be limited to ASCII letters, digits, and the
underscore character (_), all
with hex values of 0x7F or less.
·
Letters can be either uppercase or lowercase.
·
Names are not case sensitive. For example, aa and AA are
treated as the same name.
·
The first character must be either a letter or the underscore character.
·
Variable names can be thousands of characters long.
The
following four variables that are declared in the myMethod method
have valid names in X++:
X++
private void myMethod(int _, str _myParameter2)
{
str I;
str XppAllowsVeryLongVariableNamesWhichTireOurFingers;
…
}
According
to convention, variable names should begin with a lowercase letter. Variables
of specialized types should be named like these examples:
CustInvoiceJour custInvoiceJour;
CustTable custTable;
Declaration of Variables [AX
All
variables must be declared before they can be used. X++ does not allow variable
declarations to be mixed with other X++ statements; variables must be declared
before X++ statements.
The
syntax for the declaration of each variable type is described in the help
topics for the Primitive
Data Types and Composite Data Types.
When a
variable is declared, memory is also allocated and the variable is initialized
to the default value. The only exception to this is for objects, where you need
to manually allocate memory by using the new method.
For more information, see Classes
as Data Types.
Declaration With Initialization
At
times you might want a variable to have a value other than the default as soon
as the variable is declared. X++ allows you to initialize variables in the
declaration by adding an assignment statement:
// Assigns value of pi to 12 significant digits
real pi = 3.14159265359;
Another
syntax is needed to initialize objects because they are initialized by invoking
the new method
on the class:
// Simple call to the new method in the Access class
Access accessObject = new Access();
Multiple Declarations
X++
allows you to declare more than one variable in the same declaration statement.
For example:
// Declares 2 integers, i and j
int i,j;
// Declares array with 100 integers with 5 in memory
and b as integer with value 1
int a[100,5], b=1;
Summary
The
possible variable declarations in X++ are shown by using EBNF (Extended Backus
Naur Form) grammar, in the following table:
Declaration
|
=
|
Datatype Variable { , Variable } ;
|
Datatype
|
=
|
boolean | int | real | date | str | container | typeidentifier
|
Variable
|
=
|
[ Typeoptions ] Identifier [ Option ]
|
Typeoptions
|
=
|
[ Length ]
[ left | right ]
|
Option
|
=
|
Arrayoption | Initialization
|
Arrayoption
|
=
|
[ Length , Memory ]
|
Initialization
|
=
|
expression
|
typeIdentifier can be a class, an extended data type, a
table, a map, or an enumeration, which has been declared elsewhere. All of
these are declared in the Application Object Tree.
Typeoptions are
only valid for variables of type string.
Length is an
integer (or expression) that specifies the maximum length of the string or
array. If it is omitted, the string or array is dynamic.
Memory is an
integer (or expression) that specifies how many array items should be held in
memory. If it omitted, all items are held in memory.
Variable Scopes [AX 2012]
A
scope defines the area in which an item can be accessed:
·
Instance variables, declared in class declarations, can be
accessed from any methods in the class, and from methods that extend the class.
·
Local variables can be accessed only in the block in which they
were defined. All variables created by users have local scope.
Example
X++
class ANewClass
{
int a;
void aNewMethod()
{
int b;
}
}
A
variable, a, is declared in the class and
a variable, b, is declared in the aNewMethod method.
As the
method is declared in the class block, it can access all variables defined in
the class. The aNewMethod method has access to variable a.
Variable b is
declared in the aNewMethod method block, so that it can be accessed
only from within this block.
A scope defines the
area in which an item can be accessed. Variables defined in a class are
available to the methods within that class. Variables in methods can be
accessed only within the current block, as shown in the following figure.
The class A contains
methods b and c and
class variables. All methods in the class can see and use all variables in the
class.
The method b has some local variables that can only be accessed from
within method b.
Method c has some local variables, a function d, and a function e.
Functions d and e can
be seen only inside method c. As functions d and e are declared inside method c, they have access to their local variables—the local variables
in c and the variables in the class, respectively.
No comments:
Post a Comment