Using CArrayList gives error

 

Hi Guys,

Thanks for your time. I'm struggling to use the CArrayList in the generics folder. Are these interfaces and classes complete? or was CArrayList not intended to have a list  of objects?

what am I doing wrong please help?


When I compile this script below I get a couple of errors and I wasn't expecting to get any.

a description of SOME of the errors:

'CObject' - objects are passed by reference only ArrayList.mqh 450 29 ->The way to fix this in my opinion is to open ArrayList.mqh and add the '*' to the parameter but I don't want to change existing code.

'm_items' - structures or classes containing objects are not allowed ArrayList.mqh 242 22  -> it seems ArrayCopy doesnt like objects. 

'(CObject)' - invalid cast operation Introsort.mqh 158 57 


#include <Generic\ArrayList.mqh>
#include <Object.mqh>
void OnStart()
  {   
      CArrayList<CObject> *CandlePattern = new CArrayList<CObject>();
      CObject *one = new CObject();
      numbers.Add(one);
      //numbers.Add(2);
      //numbers.Add(3);
      
      Print("We have " + numbers.Count() +" Objects in the List");
  }


I don't want to change existing code because it gets overridden on updates...I learned from that mistake.


Thanks for your time and I hope this is a quick and easy one.

 
Ernie Gunning:

Are these interfaces and classes complete? or was CArrayList not intended to have a list  of objects?

In principle, yes. But you have to take care for the deallocation of objects yourself.

This can cause memory leaks, especially with RemoveRange(), when the final state doesn't expose all of the allocated instances anymore.

CArrayList<CObject *> *CandlePattern = new CArrayList<CObject *>();
 
lippmaje:

In principle, yes. But you have to take care for the deallocation of objects yourself.

This can cause memory leaks, especially with RemoveRange(), when the final state doesn't expose all of the allocated instances anymore.

Thanks ALLOT for the quick response, and agreed yes memory management can be a concern.

I felt like the list should JUST manage pointers and not values like the case with the MQL data types hence my questions above. Your suggestion below lets it be a list of pointers. Thanks YOU ARE AWESOME!!


This worked for me, specifying the '*' with the type:

CArrayList<CObject *> *CandlePattern = new CArrayList<CObject *>();
 
see also CArrayObj and CArrayDouble in Arrays
 
lippmaje:
see also CArrayObj and CArrayDouble in Arrays

Thanks again lippmaje for your quick response,

I only used CObject above to save myself from posting my own custom classes. Generally I dont like casting from CObject to my custom classes. I only recently learned MQL supports "generics" (https://www.mql5.com/en/forum/11066) and then I learned about the already generic implementation in the library today,  a long way around I know :) . I also only learned MQL supports interfaces which is awesome. I left MQL because of these limitations but since I have came back I had to let go of my old way of coding MQL and for the first time I can move to a C# like coding pattern although they are still light years apart.

In all array implementations I really missed overriding the array indicator ("[]") and I see I would need to add this to the IList interface and the implementation in CArrayList. I''m just curious as to how to do this? I can just add it in but I'm worried that future updates might override this code causing me to lose it. Would this just be a suggestion with the service desk to add this in?

Discussion of article "MQL5 Programming Basics: Arrays"
Discussion of article "MQL5 Programming Basics: Arrays"
  • 2013.03.11
  • www.mql5.com
New article MQL5 Programming Basics: Arrays is published: Author: Dmitry Fedoseev...
 
Ernie Gunning:

Generally I dont like casting from CObject to my custom classes.

For what you stated above I think a CArrayDouble would be sufficient.

CArrayDouble *candlePattern = new CArrayDouble;
double open=iOpen(_Symbol,_Period,0);
double close=iClose(_Symbol,_Period,0);
candlePattern.Add(open);
candlePattern.Add(close);
 
Ernie Gunning ...I can just add it in but I'm worried that future updates might override this code causing me to lose it.
I started using github-desktop for my versions. I can now save my codebase state, update and revert to the previous state. It's only hectic when I forget to save the state before updating, in which case I revert to the previous saved state.