In AX7 X++ now supports the const keyword. Semantically it is identical to const in C#.
In short; it allows you to define members on a class and variables in a method that can only be initialized during the declaration. The compiler will replace references to these constants with the literal value. In other words, the constant value must be known at compile time.
This is a killer feature! It replaces most use cases of macros.
There are many benefits:
- IntelliSense in the editor.
- Support for "Go to definition" in the editor.
- Full control of the type for the constant.
- Faster compilation – macros are notorious hard for the compiler to handle.
- None of the macro idiosyncrasies – like the ability to redefine/override.
static class TimeConstants
static const public int DaysPerWeek = 7;
static const public int HoursPerDay = 24;
static const public int HoursPerWeek = TimeConstants::DaysPerWeek * TimeConstants::HoursPerDay;
It is best practice to use PascalCasing when naming const variables, regardless of if they are private, protected or public.
Notice how the constants in the class are referenced:
This is much clearer than macros:
int x = #HoursPerWeek
For example; you are no longer in doubt where the definition is coming from.
THIS POST APPLIES TO MICROSOFT DYNAMICS AX7 TECHNICAL PREVIEW; IS PROVIDED AS-IS AND CONFERS NO RIGHTS.