Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1334

 
Artyom Trishkin:
It was Alexey who redid something in the code I gave. Or maybe I underestimated it - I wrote it "on my knees" as a simple example to clarify what I don't remember anymore.

Maybe Alexei can tell us.)

 
Valeriy Yastremskiy:

The iteration of the cycle will not end and it will not start a new one, but will move on to the next If. The logic will change.

No. The condition there is originally set so that if the pointer is created successfully, then add it to the list. The curve bracket is closed and if a failure occurs, addition will not be executed. The logic is a bit different with the continue operator, but the result is the same.
 

Artyom Trishkin:
Это Алексей что-то переделывал в том коде, который я давал. А может и я недоглядел - писал "на коленке" в качестве простого примера для пояснения уже не помню чего.

//+------------------------------------------------------------------+
//| CNewBar Time Возвращает время нулевого бара                      |
//+------------------------------------------------------------------+
datetime CNewBar::Time(void)
  {
   datetime array[1], ret;
   ret = CopyTime(this.m_symbol, this.m_timeframe, 0, 1, array) == 1 ? array[0] : 0;
   return(array[0]);
 }
//+------------------------------------------------------------------+
//| CNewBar IsNewBar Основная функция класса                         |
//+------------------------------------------------------------------+
bool CNewBar::IsNewBar(void)
  {
   datetime tm = this.Time();
   if(tm == 0)
      return false;
   if(tm != this.m_time)
     {
      this.m_time = tm;
      return true;
     }
   return false;
  }


Artem, why is the this keyword used here? Without it the methods will work with class members...

 
Mikhail Tkachev:

Alexey, thank you for such a detailed answer.
The purpose of ret variable in the given code fragment is not clear...
Why is it calculated if method returns array[0] anyway ?
P.S.
And why use this in a class method? We are working with members of this particular class...


  1. As function CopyTime returns a value, let it return into a variable, it won't make it worse... In case of debugging, you can check what you got.
  2. this was written by Artyom. Apparently, it is personal preferences. Some people write, must - must not write ::
ps; I just now noticed that the ret datetime variable type... Of course it should be int type. Oh well, I'll have to fix that too...
 
Alexey Viktorov:
  1. Since the CopyTime function returns a value, let it return to a variable, it won't make it worse... In case of debugging, you can check what you've got.
  2. this was written by Artyom. Apparently, it is personal preferences. Some people, it is necessary - not necessary write ::

1. what is the CopyTime function for here ?

2. Maybe Artem will answer about this - is it a personal preference or is it necessary?

 
Mikhail Tkachev:

1. what is the CopyTime function for ?

2. Maybe Artem will answer about this - is it personal preference or is it necessary ?

Don't like CopyTime(), replace it with iTime(). There is no difference, the main thing is to get the time of the current bar.

In my head I vaguely remember when it was written. I think I asked this question too... And if I'm not mistaken, this ensures that the reference is only to local methods in case there are similar ones in other classes. And of course, Artyom will clarify.

 
Alexey Viktorov:
No. The condition there is originally set like this, if the pointer is created successfully, then add it to the list. The curved bracket is closed and if there is a failure, addition will not be executed. The logic is a bit different with the continue operator but the result is the same.

Yes, the result is the same, inside is different. I think it's faster with the continuation, although it's colour and logic ....

 
Mikhail Tkachev:


Artem, why is the keyword this used here? Because even without it, methods will work with class members...

They will. I use it because I want them to :) It explicitly indicates which class methods are used.

If you have two classes, one as parent and one as descendant, and you have two methods with the same name, but they are not virtual for some reason, you have to explicitly specify which one to use.

Suppose parent class is CParent and inherited class is CInheritedand they both have Method() method.

In this case, if we call a method Method() of a CParent class, it will explicitly call a method of the CParent class - this is not required here. We can call the method with or without it.

If wecall aMethod() method in an inherited CInherited class, the methodof that class will also be called, since we first look for a method belonging to the class we called it from.

If wecall CParent::Method() in a CInherited class, it is the method of the parent class that will be called this way, because we have explicitly specified a method that belongs to the parent class by context resolution operator.

But still, I'd call method Method() in an inherited CInherited class with this - to be sure that this (this == "this") inherited class would be called.

There are other explanations for using this. Maybe someone more experienced in OOP can tell us.

 
Valeriy Yastremskiy:

Yes, the result is the same, inside is different. I think it's faster with the continuation, although it's colour and logic ....

There shouldn't be any difference. It just looks nicer to me - less brackets and indentation.

 
Alexey Viktorov:
  1. Since the CopyTime function returns a value, let it return to a variable, it won't make it worse... In case of debugging, you can check what you've got.
  2. this was written by Artyom. Apparently, it is personal preferences. Some of them write it, I don't need it ::
ps; I just now noticed that the ret datetime variable type... Of course it should be int type. But never mind, I'll have to fix it too...

Well, yes. I'm one of those "some", especially, when I call standard function from class, I always specify global context. Just because I can think of writing in this class a method with the same name as the standard function - so I'll never forget what it does. And that's where :: is useful for calling the standard function and this is useful for calling my method of the same name.

Reason: