MQL4 help needed: Access to class members in an array

MQL4 help needed: Access to class members in an array

Postby degoor1975 » Fri Jan 29, 2021 1:05 pm

I started to write an EA for the Empty4 platform and got stuck using class members in MQL4.

I come from C# background where that thing is really easy to do. Hope someone will help me doing that in MQL4.

So suppose I have a small class called #PivotPoint that has members called #Index & #PP.

Then I would like to create an array from 6 instances of the #PivotPoint class and in the body of the EA I need to add and update those instances. After that {for testing purposes} I need to create dot objects on a chart from those pivot points.

Here is a sample of my code:

Code: [Select all]
 Please Login or Register to see this content
<


As commented in the code listing I can compile that but when started on a Empty4 chart I get "invalid pointer access" error on line 65.

Obviously I can't access the members of the PivotPoint class in the array the way I am trying to. Hope you can give me a hand on that.

Technically in the code above I can add new instance of the PivotPoint class and later draw the dot by only using #index_ and #high_. The problem is that later in my code {not included here} I need to update a previous instance of that class in the array so I still need to be able to access the members of that previous instance.

EDIT:
I have missed to include a line in #OnStart about #current_chart_id variable:
Code: [Select all]
 Please Login or Register to see this content
<

but that is not essential for my question :)
In trading as in life you need to learn only from the best!
User avatar
degoor1975
 
Posts: 3
Joined: Thu Aug 29, 2019 8:05 pm
Location: Ruse, Bulgaria

MQL4 help needed: Access to class members in an array

Postby renexxxx » Fri Jan 29, 2021 10:07 pm

In order to avoid the "invalid pointer access" runtime error, you need to use the CheckPointer and GetPointer functions, as described in the docs.

Besides, if you want to create an array of object pointers, it is much easier to use the CArrayObj class included with Empty4, eg. like this:

Code: [Select all]
 Please Login or Register to see this content
<


I have no clue what your code is supposed to do and whether the above code does just that, but hopefully it helps.
User avatar
renexxxx
 
Posts: 855
Joined: Sat Dec 31, 2011 3:48 am

MQL4 help needed: Access to class members in an array

Postby degoor1975 » Sun Jan 31, 2021 8:07 am

renexxxx, thanks for your answer.

It turned out that the solution to my question is really simple:
1. When I need to create an instance of a class {as in the code above} I should do that like:
Code: [Select all]
 Please Login or Register to see this content
<

2. When I need to create an array from class instances {new here} I should code:
Code: [Select all]
 Please Login or Register to see this content
<

3. When I need to put an array from class instances as function parameter {also new here} I should code:
Code: [Select all]
 Please Login or Register to see this content
<
In trading as in life you need to learn only from the best!
User avatar
degoor1975
 
Posts: 3
Joined: Thu Aug 29, 2019 8:05 pm
Location: Ruse, Bulgaria


Return to Know your MT4 platform

Who is online

Users browsing this forum: No registered users and 1 guest