how to return an array of 2 ints?

 

Hi,

I need to write a function that returns 2 file handles, which are just ints, so I thought to declare the function like

int[] getFileHandles();

but MT5 didn't support returning an anonymous sized array, and wouldn't let me return a pointer to it or even to specify the size like

int* getFileHandles() or int[2] getFileHandles();

So I ended up creating a class with 2 int members and then having to call new to create it, then I'll have to delete it somewhere, which is all kind of heavy weight and a pain.

I must be missing the obvious easy way to do this, can someone show it to me?

thanks,

Rik

Create Your Own Trading Robot in 6 Steps!
Create Your Own Trading Robot in 6 Steps!
  • 2012.03.22
  • MetaQuotes Software Corp.
  • www.mql5.com
If you don't know how trade classes are constructed, and are scared of the words "Object Oriented Programming", then this article is for you. In fact, you do not need to know the details to write your own module of trading signals. Just follow some simple rules. All the rest will be done by the MQL5 Wizard, and you will get a ready-to-use trading robot!
 
rik:

Hi,

I need to write a function that returns 2 file handles, which are just ints, so I thought to declare the function like

int[] getFileHandles();

but MT5 didn't support returning an anonymous sized array, and wouldn't let me return a pointer to it or even to specify the size like

int* getFileHandles() or int[2] getFileHandles();

So I ended up creating a class with 2 int members and then having to call new to create it, then I'll have to delete it somewhere, which is all kind of heavy weight and a pain.

I must be missing the obvious easy way to do this, can someone show it to me?

thanks,

Rik

You have to use parameters passed by reference :

void getFileHandles(int& handle1, int& handle2)
{
  handle1 = ...
  handle2 = ...
  ...
}

You can also pass an array as parameter.

 

thanks, that will work.  Also I found https://www.mql5.com/en/articles/567 which was useful.

Rik

MQL5 Programming Basics: Arrays
MQL5 Programming Basics: Arrays
  • 2013.03.11
  • Dmitry Fedoseev
  • www.mql5.com
Arrays are an integral part of almost any programming language along with variables and functions. The article should be of interest primarily to novice MQL5 programmers, while experienced programmers will have a good opportunity to summarize and systematize their knowledge.
Reason: