I'm passing an enum to a function. In this function I want to increment the passed enum with some integer steps. I though this would be possible since an enum is supposedly an integer by definition. But for some reason the following doesn't work.
Here's my relevant code:
enum THE_ENUM {
S3,
S4,
S5,
...
};
double getPrice(THE_ENUM level) {
switch (level) {
case S3 : return mml[S3];
case S4 : return mml[S4];
case S5 : return mml[S5];
...
}
...
}
int placeOrder(THE_ENUM tPL) {
...
price = getPrice(S4); // this works
price = getPrice(tPL); // this works
price = getPrice(tPL+2); // this doesn't!
...
}
...
placeOrder(S3);
...
How can I use the passed enum to access the next or previous one, as first defined?
MQL4
is not a compiled C-langData of the enum type belong to a certain limited set of data. Defining the enumeration type:
enum name of enumerable type
{
list of values
};
The list of values is a list of identifiers of named constants separated by commas. After the enumeration is declared, a new integer-valued 4-byte data type appears. Declaration of the new data type allows the compiler to strictly control types of passed parameters, because enumeration introduces new named constants. In the above example, the January named constant has the value of 0, February - 1, December - 11.
Rule: If a certain value is not assigned to a named constant that is a member of the enumeration, its new value will be formed automatically. If it is the first member of the enumeration, the 0 value will be assigned to it. For all subsequent members, values will be calculated based on the value of the previous members by adding one.
Example:
enum intervals // Enumeration of named constants
{
month = 1, // Interval of one month ---- code-assogned = 1
two_months, // Two months ---- auto-assigned ~ 2
quarter, // Three months - a quarter ---- auto-assigned ~ 3
halfyear = 6, // Half a year ---- CODE-assigned = 6
year = 12, // Year - 12 months ---- CODE-assigned = 12
wild13, // 13 months ---- auto-assigned ~13
decade =120 // Decade ---- CODE-assigned =120
};
This means, the wish-to-have, compiler-assisted, enum
-value "arithmetics" ( asking for having a chance to call an on-the-fly evaluated/decoded in getPrice(
"second-value-before-wild13" )
is possible, yet do not expect it to be a battery-included, low-hanging fruit ready to be available from the MQL4-language architects, that have optimised the compiled-language code for efficiency and minimum latency.
One may implement one's own arithmetic for MQL4-enum
-s, as the price-level double getPrice( smart_ENUM aLevelCONST )
mapper above strives to implement, yet the costs of developing such a design, using a restricted language beyond its limits, are on one's own consideration ( do not forget a few years back surprise, when string
-s ceased to be string
-s and suddenly, without either a prior or ex-post notice, they silently become struct
-s - each Fund Manager, Trader, Support Team member or API-maintainer can imagine the shock and nightmare, once things turned wreck havoc without a chance to understand why ( the string
-> struct
notice was but in a next release of a Help-file for MQL4/MetaEditor ... too many hairs went off, countless funds went evaporated till the root-cause of the crashed factories was found hidden in a tangential notice in the Help-file and the "change" was re-factored back into the API-re-design ... We better do not expose one selves to a similarly un-manageable risk or rather do not play wild with similar "dependencies" on a moving sands territory )
At least, you've been warned :o)
Notes :
- Unlike C++, the size of the internal representation of the enumerated type in MQL4 is always equal to 4 bytes. That is,
sizeof( months )
returns the value 4.- Unlike C++, an anonymous enumeration can't be declared in MQL4. That is, a unique name must be always specified after the
enum
keyword.