Download MetaTrader 5

Problem: Putting or Pushing Value Of Variables into An Array of Same Type

To add comments, please log in or register
PCRider
26
PCRider  
Hi Dear Friends

Recently I want to make a piece of Code to Print all Information Of Account into Chart and Delete them After a while
Hence I use the Same object for All part of information So I decide to Used a for Loop to Create Object and I use two Array in that Loop, First for Object Names and Second for Property Values
The Problem is: Property Values are not Constant and We can not Put Variable into Arrays
Any Idea Or Help?

Thanks In Advance


Here is that piece of code:

#property copyright "Copyright 2017, Software Corp."
#property link      "https://www..com"
#property version   "1.00"
#property strict

//====================================================================
#include <stderror.mqh>
#include <stdlib.mqh> // both for ErrorDescription() function
//====================================================================

string gs_CopyRight           = Copyright \t\x00A9 2017, Software Corp.";
string gs_AccountCompany      = "Company:"+AccountCompany()+" Server:"+ AccountServer();
string gs_AccountOwner        = "AccountOwner: "+ AccountName()+" Login: "+ (string)AccountInfoInteger(ACCOUNT_LOGIN);
string gs_AccountBalance      = "AccountBalance: "+(string)AccountBalance()+" "+AccountCurrency();
string gs_AccountEquity       = "AccountEquity: "+(string)AccountEquity()+AccountCurrency();
string gs_AccountFreeMargin   = "AccountFreeMargin: "+(string)AccountFreeMargin()+ " " + AccountCurrency();
string gs_AccountMargin       = "AccountMargin: "+(string)AccountInfoDouble(ACCOUNT_MARGIN);
string gs_AccountCredit       = "AccountCredit: "+(string)AccountCredit()+" "+AccountCurrency();
string gs_AccountProfit       = "AccountProfit: "+(string)AccountProfit()+" "+AccountCurrency();
string gs_AccountMarginLevel  = "AccountMarginLevel: "+(string)AccountInfoDouble(ACCOUNT_MARGIN_LEVEL);
string gs_AccountMarginSoCall = "AccountMarginSoCall: "+(string)AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL);
string gs_AccountMarginSoSo   = "AccountMarginSoSo: "+(string)AccountInfoDouble(ACCOUNT_MARGIN_SO_SO);
string gs_AccountLeverage     = "AccountLeverage: "+(string)AccountInfoInteger(ACCOUNT_LEVERAGE);
string gs_AccountStopOut      = "AccountStopOut: "+(string)AccountStopoutLevel();
string gs_AccountType;

string gs_ObjectName[]=
  {
   "CopyRight","AccountCompany","AccountOwner","AccountType","AccountBalance",
   "AccountEquity","AccountFreeMargin","AccountMargin","AccountCredit","AccountProfit",
   "AccountMarginLevel","AccountMarginSoCall","AccountMarginSoSo","AccountLeverage",
   "AccountStopOut"
  };

string gs_PropertyValue[]     = {gs_CopyRight, gs_AccountCompany, gs_AccountOwner, gs_AccountType, gs_AccountBalance,
gs_AccountEquity, gs_AccountFreeMargin, gs_AccountMargin, gs_AccountCredit, gs_AccountProfit, gs_AccountMarginLevel,
gs_AccountMarginSoCall, gs_AccountMarginSoSo, gs_AccountLeverage, gs_AccountStopOut};

int OnInit()
  {
//--------------------------------------------------------------------

   switch((ENUM_ACCOUNT_TRADE_MODE)AccountInfoInteger(ACCOUNT_TRADE_MODE))
     {
      case(ACCOUNT_TRADE_MODE_DEMO):
         gs_AccountType="Demo";
         break;
      case(ACCOUNT_TRADE_MODE_CONTEST):
         gs_AccountType="Contest";
         break;
      default:
         gs_AccountType="Real";

     }
   for(int i=0;i<ArraySize(gs_ObjectName);i++)
     {
      if(!ObjectCreate(ChartID(),gs_ObjectName[i],OBJ_LABEL,0,0,0))
        {
         Print("DateTime: ",__DATETIME__," Function: ",__FUNCTION__," Line: ",__LINE__,"ObjectCreate Error: ",ErrorDescription(_LastError));
         continue;
        }
      ObjectSetString(ChartID(),gs_ObjectName[i],OBJPROP_TEXT,gs_PropertyValue[i]);
      ObjectSetString(ChartID(),gs_ObjectName[i],OBJPROP_FONT,"Arial");
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_FONTSIZE,5);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_COLOR,clrBlue);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_SELECTABLE,false);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_CORNER,CORNER_LEFT_LOWER);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_XDISTANCE,20);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_YDISTANCE,i*2*5+25);
     }
//--------------------------------------------------------------------  

   return(INIT_SUCCEEDED);
  }
//+==================================================================+
//| Expert deinitialization function                                 |
//+==================================================================+
void OnDeinit(const int reason)
  {
   for(int i=0;i<ArraySize(gs_ObjectName);i++)
     {
      if(!ObjectDelete(gs_ObjectName[i]))
        {
         Print("DateTime: ",__DATETIME__," Function: ",__FUNCTION__," Line: ",__LINE__,"ObjectDelete Error: ",ErrorDescription(_LastError));
        }
     }
  }
whroeder1
15913
whroeder1  
PCRider: The Problem is: Property Values are not Constant and We can not Put Variable into Arrays
string gs_PropertyValue[]     = {gs_CopyRight, gs_AccountCompany, gs_AccountOwner, gs_AccountType, gs_AccountBalance,
gs_AccountEquity, gs_AccountFreeMargin, gs_AccountMargin, gs_AccountCredit, gs_AccountProfit, gs_AccountMarginLevel,
gs_AccountMarginSoCall, gs_AccountMarginSoSo, gs_AccountLeverage, gs_AccountStopOut};
That's right, you can't use that format, you have to assign the non-constant values.
The enumeration makes the code self-documenting.
enum property {propCR, propComp, propOwner, propAT, propAB, propAE, propAFM,
               propMargin, propCredit, propProfit, propAML, propMSC, propSoSo,
               propLeverage, propStopOut, propCOUNT};
string gs_PropertyValue[propCOUNT];

gs_PropertyValue[propCR]         = gs_CopyRight;
gs_PropertyValue[propComp]       = gs_AccountCompany;
gs_PropertyValue[propOwner]      = gs_AccountOwner;
gs_PropertyValue[propAT]         = gs_AccountType;
gs_PropertyValue[propAB]         = gs_AccountBalance;
gs_PropertyValue[propAE]         = gs_AccountEquity;
gs_PropertyValue[propAFM]        = gs_AccountFreeMargin;
gs_PropertyValue[propMargin]     = gs_AccountMargin;
gs_PropertyValue[propCredit]     = gs_AccountCredit;
gs_PropertyValue[propProfit]     = gs_AccountProfit;
gs_PropertyValue[propAML]        = gs_AccountMarginLevel;
gs_PropertyValue[propMSC]        = gs_AccountMarginSoCall;
gs_PropertyValue[propSoSo]       = gs_AccountMarginSoSo;
gs_PropertyValue[propLeverage]   = gs_AccountLeverage;
gs_PropertyValue[propStopOut]    = gs_AccountStopOut;
PCRider
26
PCRider  
whroeder1:
PCRider: The Problem is: Property Values are not Constant and We can not Put Variable into Arrays
string gs_PropertyValue[]     = {gs_CopyRight, gs_AccountCompany, gs_AccountOwner, gs_AccountType, gs_AccountBalance,
gs_AccountEquity, gs_AccountFreeMargin, gs_AccountMargin, gs_AccountCredit, gs_AccountProfit, gs_AccountMarginLevel,
gs_AccountMarginSoCall, gs_AccountMarginSoSo, gs_AccountLeverage, gs_AccountStopOut};
That's right, you can't use that format, you have to assign the non-constant values.
The enumeration makes the code self-documenting.
enum property {propCR, propComp, propOwner, propAT, propAB, propAE, propAFM,
               propMargin, propCredit, propProfit, propAML, propMSC, propSoSo,
               propLeverage, propStopOut, propCOUNT};
string gs_PropertyValue[propCOUNT];

gs_PropertyValue[propCR]         = gs_CopyRight;
gs_PropertyValue[propComp]       = gs_AccountCompany;
gs_PropertyValue[propOwner]      = gs_AccountOwner;
gs_PropertyValue[propAT]         = gs_AccountType;
gs_PropertyValue[propAB]         = gs_AccountBalance;
gs_PropertyValue[propAE]         = gs_AccountEquity;
gs_PropertyValue[propAFM]        = gs_AccountFreeMargin;
gs_PropertyValue[propMargin]     = gs_AccountMargin;
gs_PropertyValue[propCredit]     = gs_AccountCredit;
gs_PropertyValue[propProfit]     = gs_AccountProfit;
gs_PropertyValue[propAML]        = gs_AccountMarginLevel;
gs_PropertyValue[propMSC]        = gs_AccountMarginSoCall;
gs_PropertyValue[propSoSo]       = gs_AccountMarginSoSo;
gs_PropertyValue[propLeverage]   = gs_AccountLeverage;
gs_PropertyValue[propStopOut]    = gs_AccountStopOut;
Thanks for your answer sir
But if you check it with compiler, The problem Still Remain The same
whroeder1
15913
whroeder1  
PCRider: But if you check it with compiler, The problem Still Remain The same
"Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here. Post your code.
PCRider
26
PCRider  
whroeder1:
"Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here. Post your code.
yes sir
sorry for this mistake
I just use your Code instead Of my Code
#property copyright "Copyright 2017, Software Corp."
#property link      "https://www..com"
#property version   "1.00"
#property strict

//====================================================================
#include <stderror.mqh>
#include <stdlib.mqh> // both for ErrorDescription() function
//====================================================================

string gs_CopyRight           = Copyright \t\x00A9 2017, Software Corp.";
string gs_AccountCompany      = "Company:"+AccountCompany()+" Server:"+ AccountServer();
string gs_AccountOwner        = "AccountOwner: "+ AccountName()+" Login: "+ (string)AccountInfoInteger(ACCOUNT_LOGIN);
string gs_AccountBalance      = "AccountBalance: "+(string)AccountBalance()+" "+AccountCurrency();
string gs_AccountEquity       = "AccountEquity: "+(string)AccountEquity()+AccountCurrency();
string gs_AccountFreeMargin   = "AccountFreeMargin: "+(string)AccountFreeMargin()+ " " + AccountCurrency();
string gs_AccountMargin       = "AccountMargin: "+(string)AccountInfoDouble(ACCOUNT_MARGIN);
string gs_AccountCredit       = "AccountCredit: "+(string)AccountCredit()+" "+AccountCurrency();
string gs_AccountProfit       = "AccountProfit: "+(string)AccountProfit()+" "+AccountCurrency();
string gs_AccountMarginLevel  = "AccountMarginLevel: "+(string)AccountInfoDouble(ACCOUNT_MARGIN_LEVEL);
string gs_AccountMarginSoCall = "AccountMarginSoCall: "+(string)AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL);
string gs_AccountMarginSoSo   = "AccountMarginSoSo: "+(string)AccountInfoDouble(ACCOUNT_MARGIN_SO_SO);
string gs_AccountLeverage     = "AccountLeverage: "+(string)AccountInfoInteger(ACCOUNT_LEVERAGE);
string gs_AccountStopOut      = "AccountStopOut: "+(string)AccountStopoutLevel();
string gs_AccountType;

string gs_ObjectName[]=
  {
   "CopyRight","AccountCompany","AccountOwner","AccountType","AccountBalance",
   "AccountEquity","AccountFreeMargin","AccountMargin","AccountCredit","AccountProfit",
   "AccountMarginLevel","AccountMarginSoCall","AccountMarginSoSo","AccountLeverage",
   "AccountStopOut"
  };

enum property {propCR, propComp, propOwner, propAT, propAB, propAE, propAFM,
               propMargin, propCredit, propProfit, propAML, propMSC, propSoSo,
               propLeverage, propStopOut, propCOUNT};
string gs_PropertyValue[propCOUNT];

gs_PropertyValue[propCR]         = gs_CopyRight;
gs_PropertyValue[propComp]       = gs_AccountCompany;
gs_PropertyValue[propOwner]      = gs_AccountOwner;
gs_PropertyValue[propAT]         = gs_AccountType;
gs_PropertyValue[propAB]         = gs_AccountBalance;
gs_PropertyValue[propAE]         = gs_AccountEquity;
gs_PropertyValue[propAFM]        = gs_AccountFreeMargin;
gs_PropertyValue[propMargin]     = gs_AccountMargin;
gs_PropertyValue[propCredit]     = gs_AccountCredit;
gs_PropertyValue[propProfit]     = gs_AccountProfit;
gs_PropertyValue[propAML]        = gs_AccountMarginLevel;
gs_PropertyValue[propMSC]        = gs_AccountMarginSoCall;
gs_PropertyValue[propSoSo]       = gs_AccountMarginSoSo;
gs_PropertyValue[propLeverage]   = gs_AccountLeverage;
gs_PropertyValue[propStopOut]    = gs_AccountStopOut;

int OnInit()
  {
//--------------------------------------------------------------------

   switch((ENUM_ACCOUNT_TRADE_MODE)AccountInfoInteger(ACCOUNT_TRADE_MODE))
     {
      case(ACCOUNT_TRADE_MODE_DEMO):
         gs_AccountType="Demo";
         break;
      case(ACCOUNT_TRADE_MODE_CONTEST):
         gs_AccountType="Contest";
         break;
      default:
         gs_AccountType="Real";

     }
   for(int i=0;i<ArraySize(gs_ObjectName);i++)
     {
      if(!ObjectCreate(ChartID(),gs_ObjectName[i],OBJ_LABEL,0,0,0))
        {
         Print("DateTime: ",__DATETIME__," Function: ",__FUNCTION__," Line: ",__LINE__,"ObjectCreate Error: ",ErrorDescription(_LastError));
         continue;
        }
      ObjectSetString(ChartID(),gs_ObjectName[i],OBJPROP_TEXT,gs_PropertyValue[i]);
      ObjectSetString(ChartID(),gs_ObjectName[i],OBJPROP_FONT,"Arial");
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_FONTSIZE,5);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_COLOR,clrBlue);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_SELECTABLE,false);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_CORNER,CORNER_LEFT_LOWER);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_XDISTANCE,20);
      ObjectSetInteger(ChartID(),gs_ObjectName[i],OBJPROP_YDISTANCE,i*2*5+25);
     }
//--------------------------------------------------------------------  

   return(INIT_SUCCEEDED);
  }
//+==================================================================+
//| Expert deinitialization function                                 |
//+==================================================================+
void OnDeinit(const int reason)
  {
   for(int i=0;i<ArraySize(gs_ObjectName);i++)
     {
      if(!ObjectDelete(gs_ObjectName[i]))
        {
         Print("DateTime: ",__DATETIME__," Function: ",__FUNCTION__," Line: ",__LINE__,"ObjectDelete Error: ",ErrorDescription(_LastError));
        }
     }
  }
Files:
Untitled.png 172 kb
whroeder1
15913
whroeder1  
Assignments must be inside a function.
PCRider
26
PCRider  
whroeder1:
Assignments must be inside a function.
Thank you sir
everything is fine now
To add comments, please log in or register