Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 449

 
Juer:

How can I get the value of a field of a variable of structure type, knowing the sequence number of the field in that structure.

I know that field #1 (i.e. double) has changed value. How can I use the sequence number to find this field?

Or tell me how can I get the field of the structure knowing its string name?

struct test
  {
   int num;
   double dval;
   string sval;			
  };

here i have a value

string field="dval";

I want to query this particular structure field.

 
Juer:

Or tell me, how can I get a structure field knowing its string name?

here i have a value

string field="dval";

I want to query this particular structure field.

Structures and, classes and interfaces

 

Thanks, can you tell me where the answer to my question is in there?

 
Juer:

Thanks, but can you tell me where the answer to my question is?

struct trade_settings
  {
   double take;         // значения цены фиксации прибыли
   double stop;         // значение цены защитного стопа
   uchar  slippage;     // значение допустимого проскальзывания
  };
//--- создали и проинициализировали переменную типа trade_settings
trade_settings my_set={0.0,0.0,5};  
if (input_TP>0) my_set.take=input_TP;

What's the problem, there are examples of assignment and getting value from structures.

struct test
  {
   int num;
   double dval;
   string sval;                 
  };
test Test;

//...

string field=DoubleToString(Test.dval);

You just need to convert double tostring. DoubleToString

 
Konstantin Nikitin:

What is the problem, there are examples of assignment and getting value from structures.

All you need to do is to convert double tostring. DoubleToString

What did you write and how does it solve my problem? I don't understand it.

And this was just an example. My structure has several fields of different types. And I don't know the field type, I only know the field name.

I have a string field name of the structure. I want, knowing this name, to request the corresponding field of the structure, i.e. get the value in this field.

 

you know, you're getting spoiled on the java.)

you need this.
https://www.mql5.com/ru/code/13663

JSON Serialization and Deserialization (native MQL)
JSON Serialization and Deserialization (native MQL)
  • votes: 34
  • 2015.08.18
  • o_o
  • www.mql5.com
Сериализация и десериализация JSON-протокола. Портированный код со скоростной библиотеки С++. Практичный пример: авторизация на сайте и парсинг ответа Благодарности принимаются в виде примеров, кто как применяет MQL для работы с веб-ресурсами. Поделитесь своим опытом работы с JSON в MQL. В протокол добавлены функции Escape / Unescape...
 
Juer:

What did you write and how does it solve my problem? It's not clear.

And that was just an example. My structure has several fields of different types. And I don't know the field type, I only know the field name.

I have a string field name of the structure. I want, knowing this name, to query the corresponding field of the structure, i.e. get the value in this field.

Look here, maybe this is what I need?

Forum on trading, automated trading systems and strategy testing

Can I define pyramid type programmatically ?

Anatoli Kazharski, 2015.06.17 17:07

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   double d_value =0.0;
   int    i_value =0;
   Print("d_value: type=",GetTypeName(d_value));
   Print("i_value: type=",GetTypeName(i_value));
  }
//+------------------------------------------------------------------+
//| Возвращает в строковом виде тип                                  |
//+------------------------------------------------------------------+
template<typename T>
string GetTypeName(const T &t)
  {
   return(typename(T));
  }

That is, before reading the value of a variable, get its type and then read the value into a variable of the corresponding type.
 

Would you like to ask?

https://book.mql4.com/ru/build/conditions

The function calculates the trading criteria. The function returns the following values:

10- a trade criterion for opening a market Buy order has triggered;
20- trade criterion for opening a Sell order has triggered
11- trading criterion for closing a market Buy order triggered;
21- trading criterion for closing a Sell market order has triggered; 21 - market criterion for closing a Sell order has triggered;

   if(M_0>S_0 && -M_0>Opn && St_M_0>St_S_0 && St_S_0<St_min)
      return(10);                      // Открытие Buy    
   if(M_0<S_0 &&  M_0>Opn && St_M_0<St_S_0 && St_S_0>St_max)
      return(20);                      // Открытие Sell 
   if(M_0<S_0 &&  M_0>Cls && St_M_0<St_S_0 && St_S_0>St_max)
      return(11);                      // Закрытие Buy    
   if(M_0>S_0 && -M_0>Cls && St_M_0>St_S_0 && St_S_0<St_min)
      return(21);                      // Закрытие Sell  
return 10 20 11 21 возвращаемые значения

why is the program so locked into the return values?

 
Alexey Viktorov:

Look here, maybe this is the right one.


I.e. before reading a variable's value, get its type and then read the value into a variable of the corresponding type.

And I have several fields of the same type. I don't think that's gonna help.

I found thishttps://www.mql5.com/ru/code/16282

I decided to pass its address (offset) in bytes instead of field name.

TypeToBytes
TypeToBytes
  • votes: 22
  • 2016.09.13
  • fxsaber
  • www.mql5.com
Эта кроссплатформенная библиотека позволяет удобно осуществлять побайтовую работу со структурами, массивами и стандартными типами данных. Возможности Побайтовое сравнение (== и !=) между собой структур, массивов и стандартных типов данных (в MQL по умолчанию отсутствуют операторы сравнения структур) в любом сочетании. Определение байтового...
 
Seric29:

Would you like to ask?

https://book.mql4.com/ru/build/conditions

The function calculates the trading criteria. The function returns the following values:

10- a trade criterion for opening a market Buy order has triggered;
20- trade criterion for opening a Sell order has triggered
11- trading criterion for closing a market Buy order triggered;
21- trading criterion for closing a Sell market order has triggered; 21 - market criterion for closing a Sell order has triggered;

why is the program so locked into the return values?

This cycle is a multiple repetition. There is also a branching here. When the first condition triggers, operator return is executed and program execution stops. If the condition is false, the second condition is checked, and so on. If all 4 conditions are false, the following will be executed