Is it possible to return a string array in MT4?

 

Dear MT4 programmers,

I have a function called LoadSymbolData(). But instead of coding it like:

void LoadSymbolData(string &broker_symbols)

I wonder if it can be written like that:

string LoadSymbolData() <--- return an string array

As you know MT4 is not an object-oriented language, it's a little laborious having to pass a global variable around as parameter when it should be encapsulated as a global variable in a module or private variable in a class.

Any help is much appreciated.

Scott

 

maybe need more work:

use

string LoadSymbolData()

return a string (which is a long string with StringConcatenate to Concatenate many strings with a separated char),

and then used StringFind and StringSubstr, to separate it to more strings.

it is a pity and not easy for MT to support dynamic and complex structure used in functions' parameter or return value.

 
//+------------------------------------------------------------------+
//| Purpose: answer to query https://forum.mql4.com/24488             |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void start() {
  Print("SymbolData(EURUSD,1)=",SymbolData("EURUSD",1));
  Print("SymbolData(USDJPY,2)=",SymbolData("USDJPY",2));
}

//+------------------------------------------------------------------+
//| SymbolData                                                       |
//+------------------------------------------------------------------+
double SymbolData(string sSymbol, int iCol) {
  string sSymbols[2][3]={"EURUSD","2","123",
                         "USDJPY","3","456"
                        };
  int iCnt=ArrayRange(sSymbols,0), i;
  
  for(i=0; i<iCnt; i++) {
    if(sSymbols[i][0]==sSymbol) return(StrToDouble(sSymbols[i][iCol]));
  }
  return(0);
}
//+------------------------------------------------------------------+
DxdCn offers a better oop approach, the above could give some ideas
 

You don't have to pass a global...

.

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
string array[3];
changeArray(array);
Print(array[0], array[1], array[2]);
return(0);
}

void changeArray(string &x[]){
x[0] = "Hello ";
x[1] = "World ";
x[2] = "!";
}

 
phy wrote >>

You don't have to pass a global...

.

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
string array[3];
changeArray(array);
Print(array[0], array[1], array[2]);
return(0);
}

void changeArray(string &x[]){
x[0] = "Hello ";
x[1] = "World ";
x[2] = "!";
}

phy, sxTed and Dxdcn, thanks for your input. I think Dxdcn's idea suites my special case since I am dealing with String array. Too bad we cannot even do a two-dimenion array with mixed types, otherwise I would have preferred to use a 2D array so the first can be the index column and second can the symbol column like key/value dictionary entry.

Scott

 

I checked all mode.

phy's OK but only inside MQL.

sometime ago I need use it in a DLL, but string array or any other array can not resize inside DLL. MT only support resize array inside mql, but not in DLL,

if resized inside DLL, checked in MQL, no any change can be returned or MT crasked, So can not return any array resized in DLL.

So I have to rearranged and concatenate them to a string for return.

I can not find any information about MT can not use returned Array resized inside DLL.

Maybe MQL5 can support more.

Reason: