Basic OOP questions re: CList and CObject

 

UPDATE:  I FIGURED IT OUT -- so please don't spend time analyzing the code.  Thank you!  I posted my solutions below as replies (to myself LOL...)


Hello and thanks again for all the previous help!

I'm trying to get a basic understanding of OOP before leveraging it to refactor some existing code.  I'm off to a good start but am stuck on two things.  My questions are in upper case in the comments of the code.

They are:

1) How to access the properties of CObjects stored in the CList

2) Is there a way to populate the CList with CObjects in an interative for() loop, or do I really have to create separate copies of the CObject for each?

Thanks!

//+------------------------------------------------------------------+
//|                                            expressionTester4.mq5 |
//|                                   Copyright 2022, CTB Group, LLC |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, CTB Group, LLC"
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Arrays\ArrayObj.mqh> 
#include <Arrays\ArrayDouble.mqh>
#include <Arrays\List.mqh>

input ENUM_TIMEFRAMES TF1=PERIOD_MN1;
input ENUM_TIMEFRAMES TF2=PERIOD_W1;
input ENUM_TIMEFRAMES TF3=PERIOD_D1;
input ENUM_TIMEFRAMES TF4=PERIOD_H4;
input ENUM_TIMEFRAMES TF5=PERIOD_H1;
input ENUM_TIMEFRAMES TF6=PERIOD_M15;
input ENUM_TIMEFRAMES TF7=PERIOD_M5;
input ENUM_TIMEFRAMES TF8=PERIOD_M1;


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
  
   //statusData statuses[8];
   CList *statusCList=new CList;

// IS THERE ANY WAY TO DO THIS IN AN ITERATIVE LOOP?
   // two copies of CStatuses to load into two nodes of statusCList
   CStatuses *oStatuses0=new CStatuses;
   CStatuses *oStatuses1=new CStatuses;
   // setting unique properties of each copy of CStatuses
   oStatuses0.SetTF(0);
   oStatuses0.SetTimeframe(TF1);
   oStatuses1.SetTF(1);
   oStatuses1.SetTimeframe(TF2);
   // loading each copy of CStatuses into statusCList
   statusCList.Add(oStatuses0);
   statusCList.Add(oStatuses1);
//
   
// ATTEMPTING TO ACCESS AN OBJECT IN THE LIST FOR FURTHER OPERATIONS
// The currObj and nextObj do appear in the debugger with values I assigned
   CObject *currObj=statusCList.GetNodeAtIndex(0);
   CObject *nextObj=statusCList.GetNodeAtIndex(1);
   
// HOW TO ACCESS THE currObj AND nextObj RETRIEVED FROM THE LIST?
// this works BUT I'm accessing the CArray OBJECTS directly, not the retrieved reference from the list
   Print("oStatuses0.GetTF(): ",oStatuses0.GetTF());
   Print("oStatuses0.GetTF(): ",oStatuses1.GetTF());

// I don't seem to know how to access properties of CObjects retrieved from the list with .GetNodeAtIndex() used above
// This does not compile whether public or private in class definition
//   Print("currObj.GetTF(): ",currObj.GetTF());
//   Print("nextObj.GetTF(): ",nextObj.GetTF());

// This does not complie either, whether public or private in class definition
//   int test;
//   test=currObj.TF;

   return;
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
class CStatuses: public CObject
   {
   private:
         int TF;
         string label; 
         ENUM_TIMEFRAMES timeframe; 
         int buyOrSell; 
         int ema6Handle; 
         int macdPullbackHandle; 
         bool hasPulledBack; 
         int histCount; 
         int crossCount; 
         int thickOrThin; 
         int thickOrThinCurrent;
   public:
         void SetTF(int newVal)
            {
            TF=newVal;
            }
         void SetTimeframe(ENUM_TIMEFRAMES newVal)
            {
            timeframe=newVal;
            }
         
         
         int GetTF() {return(TF);}
  
   };

 
Christian Berrigan:

Hello and thanks again for all the previous help!

I'm trying to get a basic understanding of OOP before leveraging it to refactor some existing code.  I'm off to a good start but am stuck on two things.  My questions are in upper case in the comments of the code.

They are:

1) How to access the properties of CObjects stored in the CList

2) Is there a way to populate the CList with CObjects in an interative for() loop, or do I really have to create separate copies of the CObject for each?

Thanks!

Finally found an article that helped me figure out populating the list with a loop, so I guess I just need help with accessing the properties of objects retrieved with GetNodeAtIndex().  I did set the properties of the class to public.

   CStatuses *oStatuses[2];
   for(int i=0; i<2; i++)
      {
      oStatuses[i]=new CStatuses;
      oStatuses[i].TF=i;
      }
 
Christian Berrigan #:

Finally found an article that helped me figure out populating the list with a loop, so I guess I just need help with accessing the properties of objects retrieved with GetNodeAtIndex().  I did set the properties of the class to public.

And the solution I found to accessing properties of objects retrieved from the list was in defining the variable that will hold them correctly rather than as creating a new object:

// This now works with the objects pre-defined this way:
   CStatuses currObj;
   CStatuses nextObj;
   currObj=statusCList.GetNodeAtIndex(0);
   nextObj=statusCList.GetNodeAtIndex(1);
   //CObject *currObj=statusCList.GetNodeAtIndex(0);
   //CObject *nextObj=statusCList.GetNodeAtIndex(1);