# Why define the MAGIC_NUMBER as an integer, not string?

372

I understand little bit why define the MAFIC_NUMBER as an integer, not a string? I think define it as a string is much better than as an integer since you can transform an integer to a string but nit vise verse, right? Besides a string can tell much more than an integer. Right?

Moderator
8856

thomas2004:
I understand little bit why define the MAFIC_NUMBER as an integer, not a string? I think define it as a string is much better than as an integer since you can transform an integer to a string but nit vise verse, right? Besides a string can tell much more than an integer. Right?

Just accept it, it's the way it is.

Beside that an integer can be sorted easier than a string (with or without capitals, special characters,...) and you have the comment of a position which is a string.

372

Yes, there is the comment. But can I just use the comment, not the magic number to tell the different orders? Or must I use different magic numbers as well for different order?

...
string ORDER_COMMENT = EURUSD15M;
...

...
for(int i=OrdersTotal()-1; i >= 0; i--)
{
{
...
}
}
...

A string can be understood much better than an integer.

Maybe sorting integers is easier than sorting string. But sorting string is much meaningful than sorting integer.
2392

thomas2004:

Yes, there is the comment. But can I just use the comment, not the magic number to tell the different orders? Or must I use different magic numbers as well for different order?

A string can be understood much better than an integer.

Maybe sorting integers is easier than sorting string. But sorting string is much meaningful than sorting integer.
The broker server will change the comment for things like sl, tp, close by, etc
20228

1. thomas2004: why define the MAFIC_NUMBER as an integer, not a string?
Because 15+ years ago, someone at Metaquotes defined it that way.

2. thomas2004: can I just use the comment, not the magic number to tell the different orders?
1. Not a good idea to use comments, brokers can change comments, including complete replacement.

2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

1079

It's defined as integer because otherwise it would have to be called not MAGIC_NUMBER but MAGIC_WORD and people would start writing silly things for it.

There is StringToInteger and StringToCharArray function if you really want to make things complicated though.

38701

kypa:

It's defined as integer because otherwise it would have to be called not MAGIC_NUMBER but MAGIC_WORD and people would start writing silly things for it.

There is StringToInteger and StringToCharArray function if you really want to make things complicated though.

And if it was MAGIC_WORD, people would start to ask why it's not an int or whatever.
Moderator
12729

An integer is a number and will always be a number.

But with a string there are several encoding's for example like 'UTF-8' 'Latin-1' and many more.

Errors are very easy to occur due to incompatibility between types, when these get mixed up for whatever reason.

It should be an int and it should stay an int.

The only type that i ever would consider would be a immutable bytestring but that's simply overkill in this case.

372

kypa:

It's defined as integer because otherwise it would have to be called not MAGIC_NUMBER but MAGIC_WORD and people would start writing silly things for it.

There is StringToInteger and StringToCharArray function if you really want to make things complicated though.

Cuold you please convert the "Hello World" to integer? I do want to see what it looks like. :-D