Questions on OOP in MQL5 - page 40

 
Dmitry Fedoseev:

const - if it is necessary to prohibit variable assignment (except once during initialization). If a variable is declared as conts, then when passing it to a function by reference, the function argument must also be const, but the compiler will force this, you don't have to think about it. If a variable is not being assigned, it can also be marked as const - it will work faster, this applies to function arguments. However, at any moment, you may need to modify it...

static - if it's a variable in a class, it's a rare case, even the rarest. If it is a class method, then if the method handles only arguments of this method and static variables of the class, it is also a rare case (however, not so rare, if you just collect functions into a class for convenience).

I haven't got around to trying C# DLL for MQL5 yet. I used to use C++ wrappers and created a class instance in C#. I have a suspicion that this DLL will now use a static class, which severely limits the options. Does anyone know if a static class or dynamic class is being created now?

 
Alexey Volchanskiy:

Haven't got around to trying C# DLL for MQL5 yet. I used to do it with C++ wrappers, on the plus side an instance of the class was created in C#. I have a suspicion that now the DLL will use a static class, which severely limits its possibilities. Does anyone know if it's creating a static class or dynamic class now?

I've only used static ones for MQL5 - if you have the static modifier, the signature will be visible directly from MU, very handy

about the restriction, you could throw the task into a separate thread and then nail it, it's done very fast in C#

or write calls inside the dll

it's not a problem


UPD:

I'm not sure I can explain my recent research on the rationale of using OOP in MQL tasks, but I'll tell you what I see

In general, if we use OOP style, fully respecting access control and making sure to set all private/protected/public modifiers and obligatory use const for every method definition and signature (can be removed later if used to modify data) and writing all access to protected data with get / set, the result of using OOP correctly, is the detection of big bugs on unintended datamodification during development.

I rewrote one class that way from scratch - immediately compiler found incompatibility with my problem, got into base class from descendant when checking (testing) the idea, and as usual got distracted and forgot to remove - not the fact that I would have found this bug in the test, I might have thought that it was intended)) .

Another noticeable advantage for me is the absence of all globally described variables - I try not to use them, they are not used here at all (well, except for the global optimization flag - to suit my needs and the flag of critical error when trading)

An interesting observation is that when rewriting all calls via get / set, the source class firstly increased in size and then began to shrink significantly, when I began to substitute in the calls of get / set - I do not say that it greatly inflates the code in size, but again - this will provide control over data integrity!


SZY: well, if you are a perfectionist at heart and believe that an extra call via get / set is an extra clock on CPU, then when the task is 100% done, it's not hard to count calls to each get / set via search in the source and apply an old rule - if you call a section of code more than once, then you must make this section of code as a subroutine.... nobody forbids to do the opposite ---> just one call to get / set to assign / read field.... but this is so to say "dirty tricks" with questionable benefits

 
Igor Makanu:

I only used static ones for MQL5 - if you have static modifier, the signature will be visible directly from MU, very handy

about the restriction, you could throw the task into a separate thread and then kill it.

or write calls inside the dll

I don't think that's a problem.

I don't mean static members for Sharp, I mean this

public static class ClassName {}

You can't create a static class instance via new and destroy it via delete, it has no constructors and destructors, all members and methods are static. Anyway, it's been a long time since I worked there, but I remember how much inconvenience it was, like a Procrustean bed.

They're usually used as a container for library functions, like math functions.

 
Alexey Volchanskiy:

I didn't mean static members for Sharp, but this

I think it's a class that won't see MQL without the static modifier, but it will see the methods inside the class with the static modifier

you can use any classes within dll locally, I have WinForm in a dll class without static. everything works fine, but I call functions declared with static, and where these functions are declared within the class - it does not matter for dll call from MQL, even if the entire class is static or not.

i will not be able to check it yet, i have not installed the studio on my laptop after i replaced it with SSD, i do not need it yet

 
Alexey Volchanskiy:

Haven't got around to trying C# DLL for MQL5 yet. I used to do it with C++ wrappers, on the plus side an instance of the class was created in C#. I have a suspicion that now the DLL will use a static class, which severely limits its possibilities. Does anyone know if a static class or dynamic class is being created now?

The class is regular, methods are static. I haven't tried any other options.

 
Igor, Dmitry, in short, you have to try it yourself. As I understand it, in any case, it is necessary to refer to the class name, not the name of the class object. I will try it, thanks.
 
Hello, if I have a question about signals, where should I write? I would like to know if it is reasonable to subscribe to signals, if the deposit = $312 ? And how to avoid dependence on broker and work effectively, if I started trading a month and a half ago? I started to trade a month and a half ago. Thanks.
 
Alexey Volchanskiy:
Igor, Dmitry, in short, you have to try it yourself. As far as I understand, in any case, it is necessary to refer by the class name, and not by the name of the class object. I will try it, thanks.

Address by the name of the class.

 
Alexey Volchanskiy: Igor, Dmitry, in short you have to try it yourself. I understand that in any case the class name should be referred to, not the name of the class object. I'll try it, thanks.

I cannot claim that this is the only way to call C# from MQL5, I used an example fromhttps://www.mql5.com/ru/forum/285631 developers

#import "TestLib.dll"

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int x=41;
   TestClass::Inc(x);
   Print(x);
  }

public class TestClass
{
   public static void Inc(ref int x)
   {
    x++;
   }
}

I used the developers example to get the entry point in C#, I have not used other ways, with some tinkering the same .dll code can be compiled for MT4

article and my research on why the code from the article doesn't work as intended https://www.mql5.com/ru/forum/3153/page4#comment_10366498 - everything works so far

 

looked at the results of my battle with OOP-style code - hmmm... excellent! ))))


I have a question, but in my code, well, three times exactly, I use a construction like:

private:
   COrder            *m_order;
.......

// использую так
return(CheckPointer(m_order)==POINTER_INVALID ? true : m_order.Orderclose());

//или так

if(CheckPointer(m_order)==POINTER_INVALID)

calls all in privat methods, but is there a "Jedi method ?" to get away in the source code fromCheckPointer(m_order)==POINTER_INVALID

I am asking about some get / set

no special problems, but so to speak a whim, or while the thirst for knowledge of OOP methods in C++ has not yet abated

Reason: