#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#define GV_NAME "TestGlobalVariableGet"
#define GV_VALUE 1.23
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- définit la valeur de la variable globale du terminal client nommée GV_NAME
if(!GlobalVariableSetValue(GV_NAME, GV_VALUE))
return;
//--- récupère la valeur réelle de la variable globale du terminal client avec le nom GV_NAME
//--- puisque lors de l'utilisation de la première forme d'appel de GlobalVariableGet, 0 est un signal d'erreur,
//--- il faut analyser le dernier code d'erreur lors de la lecture du résultat
double dvalue=GlobalVariableGet(GV_NAME);
if(dvalue==0 && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- affiche le résultat obtenu
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f", dvalue);
//--- définit la valeur 0 sur la variable globale du terminal client nommée GV_NAME
if(!GlobalVariableSetValue(GV_NAME, 0))
return;
//--- en utilisant la première forme de l'appel, nous obtenons la valeur booléenne de la variable globale du terminal client avec le nom GV_NAME
bool bvalue=GlobalVariableGet(GV_NAME);
if(!bvalue && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- affiche le résultat obtenu
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s", bvalue, (string)bvalue);
//--- définit la valeur non nulle sur la variable globale du terminal client nommée GV_NAME
if(!GlobalVariableSetValue(GV_NAME, GV_VALUE*100.0))
return;
//--- relit la valeur booléenne de la variable globale du terminal client nommée GV_NAME
bvalue=GlobalVariableGet(GV_NAME);
if(!bvalue && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- affiche le résultat obtenu
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s", bvalue, (string)bvalue);
//--- récupère la valeur réelle de la variable globale du terminal client portant le nom GV_NAME en utilisant la deuxième forme de l'appel GlobalVariableGet
if(!GlobalVariableGet(GV_NAME, dvalue))
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- convertit la valeur réelle résultante en un entier de type long et affiche le résultat
long lvalue=(long)dvalue;
PrintFormat("The second form of the GlobalVariableGet() function call returned the value %.2f with type long as %I64d", dvalue, lvalue);
//--- supprime la variable globale du terminal client nommée GV_NAME après utilisation
if(!GlobalVariableDel(GV_NAME))
{
Print("GlobalVariableDel() failed. Error ",GetLastError());
}
/*
résultat :
The first form of the GlobalVariableGet() function call returned the value 1.23
The first form of the GlobalVariableGet() function call returned the value 0.00 with type bool as false
The first form of the GlobalVariableGet() function call returned the value 1.00 with type bool as true
The second form of the GlobalVariableGet() function call returned the value 123.00 with type long as 123
*/
}
//+------------------------------------------------------------------+
//| Affecte la valeur à la variable globale du terminal. |
//| Si la variable n'existe pas, elle est créée. |
//+------------------------------------------------------------------+
bool GlobalVariableSetValue(const string gv_name, const double value)
{
if(GlobalVariableSet(gv_name, value)==0)
{
Print("GlobalVariableSet() failed. Error ",GetLastError());
return(false);
}
return(true);
}
|