MQL4 Book  Standard Functions  GlobalVariable Functions

GlobalVariable Functions


Many functions for working with global variables of the client terminal are described in the GlobalVariables section. In the previous section, we also mentioned that a correctly designed program had to delete its global variables when it finished running. No GVs must remain in the client terminal after all programs have been exited.

One or more GVs may remain in the terminal when debugging programs using the global variables of the client terminal. In this case, a programmer must delete GVs manually before the next start of a debugged program. To automate this process, you can create a script that deletes all the global variables of the client terminal.

GlobalVariablesDeleteAll() Function

 int GlobalVariablesDeleteAll(string prefix_name=NULL)

It deletes global variables. If a prefix is not specified for the name, then all available global variables will be deleted. Otherwise, it will delete only variables of the names that start with the specified prefix. The function returns the number of variables deleted.

Parameters:

prefix_name - prefix for the names of global variables to be deleted.

Below is an example of a simple script, deleteall.mq4, that deletes all global variables of the client terminal.

//--------------------------------------------------------------------
// deleteall.mq4
// The program is intended to be used as an example in MQL4 Tutorial.
//--------------------------------------------------------------------
int start() // Special start() function
{
GlobalVariablesDeleteAll(); // Deleting of all GVs
PlaySound("W2.wav"); // Sound
return; // Exit
}
//--------------------------------------------------------------------

The script can be started only if no program using GVs is being executed in the client terminal. Otherwise, script running may break the logic of other executed programs that may result in uncontrolled actions. After the script execution the window of global variables (F3) of the client terminal will become empty:


Fig. 153. Appearance of Global Variables window in the client terminal after the deleteall.mq4 script is executed.


Functions for Working with Global Variables


Function Summary Info
GlobalVariableCheck It returns True if a GV is available. Otherwise, it returns FALSE.
GlobalVariableDel It deletes a global variable. It returns TRUE if a variable has been successfully deleted. Otherwise, it returns FALSE.
GlobalVariableGet It returns a value of a global variable, or 0, if an error occurs.
GlobalVariableName The function returns the name of a global variable according to its index number in the global variables list.
GlobalVariableSet It sets a new value to a global variable. The system will create a new variable if there are no any already created. The time of the last access will be returned, if the function has run successfully. Otherwise, it returns 0.
GlobalVariableSetOnCondition It sets a new value to a global variable if its current value is equal to the value of the check_value third parameter. The function will generate the ERR_GLOBAL_VARIABLE_NOT_FOUND (4058) error and will return false if a variable does not exist. It returns TRUE if the function has successfully run. Otherwise, it returns FALSE.
GlobalVariablesDeleteAll It deletes global variables. If the prefix for the name is not specified, then all global variables will be deleted. Otherwise, it deletes only those of the names starting with the specified prefix. The function returns the number of variables deleted.
GlobalVariablesTotal The function returns the total number of global variables.

To get the detailed information about these and other functions, please refer to the Documentation at MQL4.community, at MetaQuotes Ltd. website, or at the "Help" section of MetaEditor.