Common structured programming constructs of X++

Completed

X++ uses classes to define data and create objects. The data that is defined in a class represents the state of the object and is stored in variables. Classes also contain methods, which will tell the object what to do.

The first piece of a class is the class declaration. The class declaration holds the name of the class, the instance variables, and other modifiers. You can determine the visibility of the variables by using three modifiers: private, protected, and public. If you do not add a modifier, it is presumed to be a public class. However, it is best practice to provide a modifier.

  • Private - Makes the variables only usable within the class that it is defined in.
  • Protected - Makes the variable only usable within the class that it is defined in and any subclass of that class.
  • Public - Makes the variable usable anywhere and is the default modifier.

The following is an example of a class declaration plus declaration of a member variable:

public class CustomerDetails
{
    private str custName;
}

Classes, unless they are static classes, need to be instantiated before they can be used. The following is an example of how to create an instance of a class TruckLoad inside another class Truck. A variable for the class TruckLoad is declared in a class method createNewTruckLoad, and then an instance of that Truckload object is created and returned to the caller by using the default constructor new followed by a return statement.

The instance method shipTruckLoad will accept an instance of the TruckLoad class to perform a shipping operation on that instance as well as a static method to calculate a total weight.

public class Truck
{
	public TruckLoad createNewTruckLoad()
	{
		TruckLoad myTruckLoad = new TruckLoad();
		return myTruckLoad;
	}
	public void shipTruckLoad(TruckLoad _truckLoad)
	{
		_truckLoad.ship();
	}
	public static int calcTotalWeight(int  netWeight,int tareWeight)
	{
		return netWeight+tareWeight;
	}
}

Methods can be as follows:

  • Instance methods (or object methods) - Are embedded in the object that was created from a class. The object must be instantiated before you can use this method. An instance method can access the instance and static state, whereas a static method can only access the static state.

  • Static methods (also known as class methods) - You do not need to instantiate an object to use these methods. You can declare a method as static with the keyword static. A specific type of static method is the Main method, which can be called directly from a menu option.

Methods are made up of a signature and a body. The method header holds the method's name, return type, method modifiers, and parameters. If there is no return type, then the keyword void is used. The body holds the variable declarations, method declarations, and statements.

The following is an example of a method. The method is a public method that returns a real data type. The method is called ReturnCalculation. It also has a real data type parameter _expression1. Parameters should begin with "_" to easily identify parameters within the code.

public real ReturnCalculation(real _expression1)
{
	real expression2 = 2;
	return _expression1 * expression2;
}

Now, you can collect all the different pieces of a class to create your own class.