Object type needed, Does Generics work in MQL5???

 

I had the need to store different objects types (including value types) in one list or array. I struggled to do this in this c++ like language MQL5. How can I do this?

I needed something like this :

Object myObjects[10];
int point = 2;
string symbol = "Test";
myObject[0] = point;
myObject[1] = symbol;

This doesn't seem possible? My next alternative was to create a generic class...I'm afraid I might be hoping for the impossible? I don't see generics?

Luckily I managed to get past my problem but this was only because of my situation I'm in and I believe their might be people that want to do this??

Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Objects Constants / Object Properties - Documentation on MQL5
 
See Classes of data it may help you.

 
ToolMaker:

I had the need to store different objects types (including value types) in one list or array. I struggled to do this in this c++ like language MQL5. How can I do this?

I needed something like this :

Object myObjects[10];
int point = 2;
string symbol = "Test";
myObject[0] = point;
myObject[1] = symbol;

This doesn't seem possible? My next alternative was to create a generic class...I'm afraid I might be hoping for the impossible? I don't see generics?

Luckily I managed to get past my problem but this was only because of my situation I'm in and I believe their might be people that want to do this??

 

You seem to be looking for a 'Variant' data type like in Visual Basic.  This doesn't exist in C++ or MQL5, nor the legacy 'union' data construct from C days.  But it would be possible to code up what you're looking for in MQL5 if you write a number of very simple classes derived from CObject, and a generic Object array class derived from CArrayObj.  

Ideally operator overloading would make the resultant code as clean as your sample, but MQL5 unfortunately doesn't support it at this stage.

 Paul