Questions on OOP in MQL5 - page 33

 
Alexey Viktorov:

It is no secret that the economic calendar event sample is CalendarValueHistory(). In one case all news in a time range. In another by country and another time range. In the third one by a symbol. But is there a fundamental difference? Do programming lectures divide solutions according to the type of tasks they perform?

I recently met someone who didn't pass his driving test and was indignant, saying that they didn't drive on this intersection in their driving lessons and he just doesn't know how to drive on this intersection.

When you create a class, you are introducing a new type of data.

What exactly your class does is not so important.

What's important is what you plan to do with the class objects as data?

(Put into an array, pass into functions, return from functions, ...).

At this point, I'm not quite sure whether this class is even necessary.

And if it is, must it necessarily be one class instead of three?

Maybe in your task you can get along with one (or three) functions?

 
Alexey Viktorov:

Boredom... There are also templates with interfaces to master???

interfaces as such do not exist in MQL, a couple of pages ago with an abstract class would be the same, it is easier for me to just read the code afterwards

interfaces are convenient to initialize with different constructors

the example in the helphttps://www.mql5.com/ru/docs/basis/types/classes by the keyword interface is one in the same as my template, try to reproduce the example in your own place, maybe that's what you're looking for

 
Igor Makanu:

interfaces as such do not exist in MQL, a couple of pages ago with an abstract class would be the same, it is easier for me to just read the code afterwards

interfaces are convenient to initialize with different constructors

the example in the helphttps://www.mql5.com/ru/docs/basis/types/classes by the keyword interface - this is exactly my template, try to reproduce the example in your own environment, maybe this is what you're looking for

Igor, do you remember the task the Tsar gave to Fedot the Streltsy?

     Try and get me that To-Can't-Be! Write down the name, lest you forget it in your haste!

And what was the answer of the two men of character?

     If only we had a schematic or a blueprint, we'd have a blueprint, but if not, we'd look for it and find the devil!

How can you try to reproduce something if you don't know the rules or the final result to be obtained...

 
Alexey Viktorov:

Boring... There are templates and interfaces to be mastered as well???

Well, if it's boring, it's not your thing.
 
Koldun Zloy:

When you create a class, you are introducing a new data type.

It is not yet clear whether this class is needed at all.

And if it is, does it have to be one class instead of three.

Maybe in your task you can get by with one (or three) functions?

It may be possible, and most likely it is possible, but I would like to write a class which can be connected to both indicator and Expert Advisor. And the indicator, from my point of view, is designed so that the function should be called in different variants.

 
Alexey Viktorov:

It may be possible to do it, and most likely you can, but it would be good to write a class which can be called from an indicator and from an Expert Advisor. And the indicator, in my opinion, is designed in such a way that the function should be called in different variants.

The function can also be called from the indicator and from the Expert Advisor.

 
Alexey Viktorov:

I think I'm beginning to understand the necessity and usefulness of OOP, but I'm having trouble implementing it.

I have a class that is to be used with three different sets of variables. But it performs one task. In simple terms, we can insert 3 constructors, declare 3 variables and refer to them. But as I see it, it is not quite correct. On top of that, there is one variable of the string type in two variants but it is different in name and is used in different parts of the code. Of course, you may change the sequence of variables but I think this is not quite correct either.

I read the documentation about the new operator but do not understand how it may help. I do not see the difference between three different object variables and three pointers to the same objects. Perhaps it's profitable when you create a pointer once, use this object and delete it when it is not needed. But if you need the object regularly, it's absolutely silly to create a pointer to it each time and delete it.

So, please give me a hand. I've read some explanations of OOP in C++, but they are described there in a worse way than in the documentation on mql5. Please do not ask the impatient ones to join me, I will ask too many questions. I'm not interested in ready-made code without explanations either, I want to understand it, and not to stupidly repeat "Do with us, do as we do...".

The closest entity to the concept of "Class" is structure. And a structure is a linked set of data. That is, it is a group of heterogeneous variables that are linked together in a meaningful way. A class also adds methods, which can be used to manipulate this data. The closest analogue of a "Method" is a function. I.e. a class is a structure with a set of functions built into it, which can be used to control and form the data that are inside it. There can be several methods for different situations and variants of forming data inside the class object. In your case, you will need three methods, which will form data inside the class, accordingly for each situation.
There must be a "default constructor" in the class. This thing is called when a new object of the class is created using the new operator. I would compare the default constructor to the OnInit function in an MQL program.
The destructor is, to continue the analogy, an analogue of the OnDeinit function.
You don't have to "kill" the object right after it has been created and done its job. You can "kill" created objects in the OnDeinit function after the MQL-program is finished, and while the program is running, all objects can be in memory and you can refer to them.
 
Koldun Zloy:

The function can also be called from both the indicator and the EA.

I don't doubt that. I wanted to join the trendy ways of programming, and you give me such advice)))). You may as well refuse from OOP. I already don't understand the need for these gimmicks and now you talk me out of it :)))

 
Alexey Viktorov:

I don't doubt that. I wanted to join the trendy ways of programming, and you give me this kind of advice.) You may as well refuse to use OOP at all. I already don't understand the need for these gimmicks and now you talk me out of it :)))

If you're using classes for something you don't understand, it's not OOP.

And yes, I advise you, until you understand the need for these gimmicks, don't use them.

 
BlackTomcat:
The closest entity to the concept of "Class" is structure. And a structure is a linked set of data. That is, it is a group of heterogeneous variables that are linked together in a meaningful way. A class also adds methods, which can be used to manipulate this data. The closest analogue of a "Method" is a function. I.e. a class is a structure with a set of functions built into it, which can be used to control and form the data that are inside it. There can be several methods for different situations and variants of forming data inside the class object. In your case, you will need three methods, which will form the data inside the class accordingly for each situation.
There must be a "default constructor" in the class. This thing is called when a new object of the class is created using the new operator. I would compare the default constructor to the OnInit function in an MQL program.
The destructor is, to continue the analogy, an analogue of the OnDeinit function.
You don't have to "kill" the object right after it has been created and done its job. You can "kill" created objects in the OnDeinit function after the MQL-program is finished, and while the program is running, all objects can be in memory and you can refer to them.

So, I'm trying to avoid using three similar methods. After all, they are almost the same. They get an array of calendar events and sifting out unnecessary records, they provide an array for further processing.

Reason: