Saturday, September 20, 2014

Method overloading in Ax 2012

Method overloading:

Method overloading is the one of the oops concept. Method overloading is same method name but different parameters or different parameter data types in a class.

For example:

Private real GetSalary(int empId)
{
…………….
}

Private real GetSalary(int empId, boolean plusBonus)
{
……
}

The above methods are same name “GetSalary” but parameters are different. We can call this method like below

salaryAmount = GetSalary(3426);

or

salaryAmount = GetSalary(3426, true);

Unfortunately, X++ does n’t support method overloading, but it supports options parameters. The optional parameter mimics like method overloading. In X++ we can do the above business logic like below code by using optional parameters.

Private real GetSalary(int empId, boolean plusBonus = false)
{
……………
}

Here, plusBonus variable is the optional parameters.

We can call the above method like below code

salaryAmount = GetSalary(3426); // The default value “False” will be set to plusBonus parameter

or

salaryAmount = GetSalary(3426, true);



We can order optional parameters to end parameter(s). In other words the optional parameters should be placed end to the non optional parameters. We can't place non optional parameters next to optional parameters. For your reference please refer the below code. The below code is not supported.

Private real GetSalary(boolean plusBonus = false, int empId)
{
……………
}


Note: C# supports both method overloading and optional parameter concepts.

No comments :

Post a Comment