Using <Arrays\ArrayObj.mqh> stdlib

 

Hello,

I'm having difficulties using this library and I need someone to point me in the right direction.

I'm trying to create an array full of CCandle references, print the contents, change one of the values and then print the contents again to display the modified value. This the code I have at present (I'm using it as a script for test purposes):

//+------------------------------------------------------------------+
//|                                                    TestClass.mq4 |
//|                                                Copyright 2024, X |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, X"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <Arrays\ArrayObj.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CCandle : public CObject
  {
public:
   void              CCandle() : m_value(INT_MIN) {};
   void             ~CCandle() {};
   void              CCandle(const int value) : m_value(value) {};
   void              set_value(const int value)
     {
      m_value = value;
     };
   int               get_value() const
     {
      return m_value;
     };

private:
   int               m_value;
  };

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CFooBar
  {
public:
   void              CFooBar(void) {};
   void             ~CFooBar(void);
   void              CFooBar(const int shift);
   void              change(const int index, const int new_value);
   void              print(void);

private:
   CArrayObj*        m_object;
  };

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CFooBar::~CFooBar(void)
  {
   for(int i = 0; i < m_object.Total(); i++)
     {
      CCandle* obj = m_object.At(i);
      delete(obj);
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CFooBar::CFooBar(const int shift)
  {
// Just define 3 randon objects
   CCandle* obj = new CCandle(shift);
   m_object.Add(obj);
   obj = new CCandle(shift+1);
   m_object.Add(obj);
   obj = new CCandle(shift+2);
   m_object.Add(obj);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CFooBar::change(const int index, const int new_value)
  {
   if(index < m_object.Total())
     {
      CCandle* obj = m_object.At(index);
      obj.set_value(new_value);
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CFooBar::print(void)
  {
   for(int i = 0; i < m_object.Total(); i++)
     {
      CCandle* obj = m_object.At(i);
      PrintFormat("i = " + IntegerToString(i) + " " + IntegerToString(obj.get_value()));
     }
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int shift = 1;
   CFooBar* fb = new CFooBar(1);

   PrintFormat("Before Change");
   fb.print();
   fb.change(2, 5);
   PrintFormat("After Change");
   fb.print();
   delete(fb);
  }

//+------------------------------------------------------------------+

Could someone explain to me what I am doing wrong, giving examples please.

Thank you.

 

Seems like I've fixed it with this code:

//+------------------------------------------------------------------+
//|                                                    TestClass.mq4 |
//|                                                Copyright 2024, X |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, X"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <Arrays\ArrayObj.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CCandle : public CObject
  {
public:
   void              CCandle() : m_value(INT_MIN) {};
   void             ~CCandle() {};
   void              CCandle(const int value) : m_value(value) {};
   void              set_value(const int value)
     {
      m_value = value;
     };
   int               get_value() const
     {
      return m_value;
     };

private:
   int               m_value;
  };

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CFooBar
  {
public:
   void              CFooBar(void) { m_object = new CArrayObj(); };
   void             ~CFooBar(void);
   void              CFooBar(const int shift);
   void              change(const int index, const int new_value);
   void              print(void);

private:
   CArrayObj*        m_object;
  };

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CFooBar::~CFooBar(void)
  {
   for(int i = 0; i < m_object.Total(); i++)
     {
      CCandle* obj = m_object.At(i);
      delete(obj);
     }
   delete m_object; 
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CFooBar::CFooBar(const int shift)
  {
// Just define 3 random objects
   m_object = new CArrayObj();
   CCandle* obj = new CCandle(shift);
   m_object.Add(obj);
   obj = new CCandle(shift+1);
   m_object.Add(obj);
   obj = new CCandle(shift+2);
   m_object.Add(obj);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CFooBar::change(const int index, const int new_value)
  {
   if(index < m_object.Total())
     {
      CCandle* obj = m_object.At(index);
      obj.set_value(new_value);
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CFooBar::print(void)
  {
   for(int i = 0; i < m_object.Total(); i++)
     {
      CCandle* obj = m_object.At(i);
      PrintFormat("i = " + IntegerToString(i) + " " + IntegerToString(obj.get_value()));
     }
  }
  
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int shift = 1;
   CFooBar* fb = new CFooBar(1);

   PrintFormat("Before Change");
   fb.print();
   fb.change(2, 5);
   PrintFormat("After Change");
   fb.print();
   delete(fb);
  }

//+------------------------------------------------------------------+

Thanks for your time.