Passing array of user-defined-class as reference

 

Hi,

I have a user-defined-class as follow:

class setup
  {
protected:

MY_ORDER_STATE    xOrderState;

public:
   //Parametric Constructor
                     setup(double FrPrice, datetime FrTime, ENUM_XBARS_MODE Mode);
   //Constructor
                     setup(void);
   //Destructor
                    ~setup(void);

MY_ORDER_STATE    Get_Order_State(void);            //Get Current Order State

....

and I managed to declare it as global array,

setup *BuyPendingSetup[]; setup *SellPendingSetup[];
setup *BuyRunningSetup[]; setup *SellRunningSetup[];

and I am able to access the array by declaring custom function to each array:

void BuyPendingReset(void){
   if (ArraySize(BuyPendingSetup)>0){
      setup *tmp = BuyPendingSetup[0];
      MY_ORDER_STATE statecheck = tmp.Get_Order_State();
      
      if (statecheck == MY_ORDER_PENDING){}//delete order
      else if (statecheck == MY_ORDER_OPENED) {} //move to running
      else {Print("Something wrong ... Pending Reset Error");}
      delete(tmp);
      ArrayResize(BuyPendingSetup,0);}}
      
void SellPendingReset(void){
   if (ArraySize(SellPendingSetup)>0){
      setup *tmp = SellPendingSetup[0];
      MY_ORDER_STATE statecheck = tmp.Get_Order_State();
      
      if (statecheck == MY_ORDER_PENDING){}//delete order
      else if (statecheck == MY_ORDER_OPENED) {} //move to running
      else {Print("Something wrong ... Pending Reset Error");}
      delete(tmp);
      ArrayResize(SellPendingSetup,0);}}

I try to simplify the function into one by doing the following:

void PendingReset(setup &PendingSetup[]){
   if (ArraySize(PendingSetup)>0){
      setup *tmp = PendingSetup[0];
      MY_ORDER_STATE statecheck = tmp.Get_Order_State();
      
      if (statecheck == MY_ORDER_PENDING){}//delete order
      else if (statecheck == MY_ORDER_OPENED) {} //move to running
      else {Print("Something wrong ... Pending Reset Error");}
      delete(tmp);
      ArrayResize(PendingSetup,0);}}

But I have encountered errors as follow when called the below function:

'BuyPendingSetup' - parameter conversion not allowed SR Study v1.mq4 74 26

'SellPendingSetup' - parameter conversion not allowed SR Study v1.mq4 85 26

PendingReset(SellPendingSetup);
PendingReset(BuyPendingSetup);

Appreciate if the following help could be provided:

1. The right way to code to pass user-defined-class array as parameter to function 

2. Documentation related to passing user-defined-class as array

 
TeeZai:

Hi,

I have a user-defined-class as follow:

and I managed to declare it as global array,

and I am able to access the array by declaring custom function to each array:

I try to simplify the function into one by doing the following:

But I have encountered errors as follow when called the below function:

'BuyPendingSetup' - parameter conversion not allowed SR Study v1.mq4 74 26

'SellPendingSetup' - parameter conversion not allowed SR Study v1.mq4 85 26

Appreciate if the following help could be provided:

1. The right way to code to pass user-defined-class array as parameter to function 

2. Documentation related to passing user-defined-class as array

setup *BuyPendingSetup[]; setup *SellPendingSetup[];
setup *BuyRunningSetup[]; setup *SellRunningSetup[];

this is not declaration of global array of objects but declaration of array of pointers to must be previously constructed array/non array of object

void PendingReset(setup &PendingSetup[])


this will work if you pass array of existing objects as reference, what you passed is pointer

class A
{
      int          numbers;     
      public:
                   A() { numbers = 0;}
                   void SetNo(int no)    {numbers=no;};
                   int GetNo(){return(numbers);};
};
A aTest[];
A* atestPtr;
A bTest[];
A *bTestPtr;
A *cTest[];
int OnInit()
  {

    for (int i=0;i<6;i++)
    {
        
        ArrayResize(aTest,i+1);
        atestPtr = GetPointer(aTest[i]);
        atestPtr.SetNo(i);
        
        ArrayResize(bTest,i+1);
        bTestPtr = GetPointer(bTest[i]);
        bTestPtr.SetNo(i+1);
        
    }
    
    Print("================================================================================");
    
    for (int j=0;j<ArraySize(aTest);j++)
    {
        Print("value of aTest",IntegerToString(j)," is ",aTest[j].GetNo());
    }
    Print("================================================================================");
    for (int k=0;k<ArraySize(bTest);k++)
    {
        Print("value of bTest",IntegerToString(k)," is ",bTest[k].GetNo());
    }
    Print("================================================================================");
    for (int l=0;l<ArraySize(bTest);l++)
    {
        ArrayResize(cTest,l+1);
        cTest[l] = GetPointer(bTest[l]);
        cTest[l].SetNo(l+2);
    }
    Print("================================================================================");
    for (int m=0;m<ArraySize(bTest);m++)
    {
        
        Print("new value of bTest",IntegerToString(m)," changed by cTest",IntegerToString(m)," is ",cTest[m].GetNo());
    }

    ResetValue(aTest,"aTest");
    ResetValue(bTest,"bTest");
    ChangeValue(cTest,"bTest","cTest");
   return(INIT_SUCCEEDED);
  }
  
void ResetValue(A &test[],const string objname)
{
    Print("================================================================================");
    for (int k=0;k<ArraySize(test);k++)
    {
        test[k].SetNo(0);
        Print("now value of ",objname,IntegerToString(k)," is ",test[k].GetNo());
    }
}

void ChangeValue(A* &test[],const string objname,const string ptrname)
{
    Print("================================================================================");
    for (int k=0;k<ArraySize(test);k++)
    {
        test[k].SetNo(3);
        Print("new value ",objname,IntegerToString(k)," changed by ",ptrname,IntegerToString(k)," is ",test[k].GetNo());
    }
}
see  that above example if that can help you
 
Sardion Maranatha:

this is not declaration of global array of objects but declaration of array of pointers to must be previously constructed array/non array of object


this will work if you pass array of existing objects as reference, what you passed is pointer

see  that above example if that can help you
It works like charm! Thank you very much for the explanation.
 
Have you tried:
void PendingReset(setup* &PendingSetup[]){
A reference to an array of pointers.
 
William Roeder:
Have you tried: A reference to an array of pointers.
Yes, this resolved my issue perfectly.  Maranatha has given me a picture to look for Object Pointers. Thank you!
Reason: