Nested Struct / Global Variable Class(or library)

 

Hi,

Read the following https://www.mql5.com/en/forum/168741

It says that i cant have a nested struct. I wast trying something like the following.

struct STRUCT_GV
   {
      string   Name;                   // GV Name
      double   Value;                  // GV Value
      double   Time;                   // GV Time Last Accessed
   
   };
struct STRUCT_GV_PAIR
  {
      STRUCT_GV   Name;                // Pair Name
      STRUCT_GV   Pair;                // Pair Number
      STRUCT_GV   iMargin;             // Initial Margin
      STRUCT_GV   mMargin;             // Maintenance Margin
      STRUCT_GV   Direction;           // Direction
  };

Intending to use

STRUCT_GV_PAIR EURUSD[3];
EURUSD[0].Direction.Value=1.0;  // This line gives compile error.


The motive is to work easier with Global Variables.  I do see the GlobalVariableXXXX() functions... but I am trying to understand if there are best practices / libraries for working with Global Variables.


Global Variables appears to be a method to have inter EA communication within same account. (Please enlighten if otherwise).


Any guidance?

Why can't you encapsulate a nested struct or class?
Why can't you encapsulate a nested struct or class?
  • 2017.02.01
  • www.mql5.com
Is there a reason why you cannot encapsulate a nested struct or class like in C++? Here is an example. Output: 10...
 

How about Enumerations ? 

enum gv_type
{
gvt_buy=0,//Buy
gvt_sell=1,//Sell
gvt_buy_stop=2,//Buy Stop
gvt_sell_stop=3,//Sell Stop
gvt_buy_limit=4,//Buy Limit 
gvt_sell_limit=5,//Sell Limit
gvt_deposit=6,//Deposit
gvt_withdrawal=7//Withdrawal
};

struct GV_PAIR
{
string Symbol;
gv_type Type;
double Lot;
datetime OpenTime;
double StopLoss;
double TakeProfit;
};
 

Agree that enumerations are a way to deal with Global Variables (GV).

I probably have to "enum Symbol" instead of "string symbol" as well if i want to store symbol in the value of GV.

Need to test if i need to "double to int" before comparing value stored in GV to the enum.



I suppose i need to work around it as such.  Code dont feel right..... but a work around nonetheless...


struct STRUCT_GV_PAIR_NAME
  {
      string   Name;                // Pair Name
      string   Pair;                // Pair Number
      string   iMargin;             // Initial Margin
      string   mMargin;             // Maintenance Margin
      string   Direction;           // Direction
  };

struct STRUCT_GV_PAIR_VALUE
  {
      double   Name;                // Pair Name
      double   Pair;                // Pair Number
      double   iMargin;             // Initial Margin
      double   mMargin;             // Maintenance Margin
      double   Direction;           // Direction
  };

 
Keith Long:

Hi,

Read the following https://www.mql5.com/en/forum/168741

It says that i cant have a nested struct.

No it doesn't say that. It says you can't have a private struct inside a class, it's always public unlike C++.

I wast trying something like the following.

The code you posted compiles just fine.

 
struct STRUCT_GV
   {
      string   Name;                   // GV Name
      double   Value;                  // GV Value
      double   Time;                   // GV Time Last Accessed
   
   };
struct STRUCT_GV_PAIR
  {
      STRUCT_GV   Name;                // Pair Name
      STRUCT_GV   Pair;                // Pair Number
      STRUCT_GV   iMargin;             // Initial Margin
      STRUCT_GV   mMargin;             // Maintenance Margin
      STRUCT_GV   Direction;           // Direction
  };

Try that in Visual Studio as a Struct. Your going to get errors and warnings.

 

Guys,


My bad. Alain is right.

It compiles all right.


If I used the following in OnInit, it compiles okay.  I used it in the variable definition portion, causing the error.  My bad. Sorrie.

EURUSD[0].Direction.Value=1.0;
Reason: