Variable Declaration and Initialization in X++ Programming Language

Rumman Ansari   Software Engineer   2023-08-23   9207 Share
☰ Table of Contents

Table of Content:


What is variable?

variable is an identifier that points to a memory location where information of a specific data type is stored. The size, precision, default value, implicit and explicit conversion functions, and range depend on the variable's data type.

variable in c

Important points about variables in X++

  • The scope of a variable defines the area in the code where an item can be accessed.
  • Instance variables are declared in class declarations, and can be accessed from any methods in the class or from methods that extend the class.
  • Local variables can be accessed only in the block where they were defined.
  • When a variable is declared, memory is allocated, and the variable is initialized to the default value.
  • You can assign values to both static fields and instance fields as part of the declaration statement.
  • Variables can be declared anywhere in a code block in a method. They don't have to be declared at the beginning of a method.
  • Constants are variables where the value can't be changed when the variable is declared. They use the const or readonly keyword.
  • Constants differ from read-only fields in only one way. Read-only fields can be assigned a value only one time, and that value never changes. The field can be assigned its value either inline, at the place where the field is declared, or in the constructor.

The syntax rules for variable names are as follows:

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++:


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; 

Examples

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, p and q
int p, q;

// Declares array with 100 integers with 5 in memory and k as integer with value 1

int a[100,5], k = 1;

An example of an assignment of field members inline.


// An example of an assignment of field members inline.
public class FieldAssignmentExample
{
    int field1 = 1;
    str field2 = "Banana";
    void new()
    {
        // ...
    }

An example of a constant being declared at the class level.


class ConstantExample
{
    // An example of a constant being declared at the class level.
    public const str MyContent = 'SomeValue';
    public int ResultSoFar()
    {
        return 1;
    }
}

The constants can then be referenced by using the double-colon syntax.


// The constants can then be referenced by using the double-colon syntax.
str value = ConstantExample::MyContent;
// If you're in the scope of the class where the const is defined,
// you can omit the type name prefix (ConstantExample in this example).

An example of the using clause where the alias can denote


// An example of the using clause where the alias can denote
// namespaces and classes.
using System;
using IONS=System.IO; // Namespace alias.
using Alist=System.Collections.ArrayList; // Class alias.
public class NamespaceExample
{
    public static void Main(Args a)
    {
        Int32 I; // Alternative to System.Int32.
        Alist al; // Using a class alias.
        al = new Alist();
        str s;
        al.Add(1);
        IONS.Path::ChangeExtension(@"c:\tmp\test.xml", ".txt");
    }
}

X++ follows the naming conventions of the .NET framework

X++ follows the naming conventions of the .NET framework, which means that variable names must begin with a letter or an underscore, and can contain letters, numbers, and underscores. They should also be descriptive and meaningful, using camelCase notation. For example, "myVariable" is a valid variable name, but "my_variable" is not.

It's also worth noting that, X++ has some reserved keywords that cannot be used as variable names, for example, keywords such as "class", "int", "new", "while" etc.

Additionally, it's a good practice to use meaningful and descriptive variable names, that indicate the purpose of the variable, this will help you to maintain and understand your code better. Also, it's important to follow a consistent naming convention throughout your codebase, this will make it easier for other developers to understand and work with your code.