#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
//+------------------------------------------------------------------+
//| Skript Programm Start Funktion |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Setzen des Werts der globalen Variablen des Client-Terminals namens GV_NAME
if(!GlobalVariableSetValue(GV_NAME, GV_VALUE))
return;
//--- Den tatsächlichen Wert der globalen Variablen des Client-Terminals mit dem Namen GV_NAME abrufen
//--- da bei Verwendung der ersten Form des Aufrufs von GlobalVariableGet Null das Signal eines Fehlers ist,
//--- muss der letzte Fehlercode beim Lesen des Ergebnisses analysiert werden.
double dvalue=GlobalVariableGet(GV_NAME);
if(dvalue==0 && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- Anzeige des erhaltenen Ergebnisses
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f", dvalue);
//--- Setzen des Nullwerts für die globale Variable des Client-Terminals namens GV_NAME
if(!GlobalVariableSetValue(GV_NAME, 0))
return;
//--- Mit der ersten Form des Aufrufs erhalten wir den booleschen Wert der globalen Variablen des Client-Terminals mit dem Namen GV_NAME
bool bvalue=GlobalVariableGet(GV_NAME);
if(!bvalue && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- Anzeige des erhaltenen Ergebnisses
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s", bvalue, (string)bvalue);
//--- Zuweisen eines Werts ungleich Null auf die globale Variable des Client-Terminals namens GV_NAME
if(!GlobalVariableSetValue(GV_NAME, GV_VALUE*100.0))
return;
//--- Erneutes Lesen des booleschen Wertes der globalen Variablen GV_NAME des Client-Terminals.
bvalue=GlobalVariableGet(GV_NAME);
if(!bvalue && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- Anzeige des erhaltenen Ergebnisses
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s", bvalue, (string)bvalue);
//--- Den tatsächlichen Wert der globalen Variablen des Client-Terminals mit dem Namen GV_NAME mithilfe der zweiten Form des Aufrufs GlobalVariableGet abrufen
if(!GlobalVariableGet(GV_NAME, dvalue))
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- Konvertieren des resultierenden reellen Werts in eine Ganzzahl vom Typ long und anzeigen des Ergebnisses
long lvalue=(long)dvalue;
PrintFormat("The second form of the GlobalVariableGet() function call returned the value %.2f with type long as %I64d", dvalue, lvalue);
//--- Löschen der globalen Variablen GV_NAME des Client-Terminals nach dessen Verwendung
if(!GlobalVariableDel(GV_NAME))
{
Print("GlobalVariableDel() failed. Error ",GetLastError());
}
/*
Ergebnis:
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
*/
}
//+------------------------------------------------------------------+
//| Setzen des Werts auf die globale Variable des Terminals. |
//| Ist keine Variable vorhanden ist, wird sie erstellt. |
//+------------------------------------------------------------------+
bool GlobalVariableSetValue(const string gv_name, const double value)
{
if(GlobalVariableSet(gv_name, value)==0)
{
Print("GlobalVariableSet() failed. Error ",GetLastError());
return(false);
}
return(true);
}
|