GlobalVariableName

通过序列号返回全局变量名称。

string  GlobalVariableName(
   int  index      // 全局变量列表中全局变量数
   );

参量

index

[in]  全局变量列表中的序号,应该大于或等于0,小于 GlobalVariablesTotal()函数。

返回值

全局变量的名称通过序列号在全局变量列表中体现出来,更多关于误差的细节,调用 GetLastError()

注释

全局变量在客户端保存自最后使用的四周,然后自动删除。

 

示例:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   GV_NAME    "TestGlobalVariableSet"
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 首先,让我们创建全局变量
   for(int i=0i<21i++)
      GlobalVariableSet(GV_NAME+string(i),i);
//--- 获取客户端的全局变量数量,并在循环中显示它们的名称
   int total=GlobalVariablesTotal();
   for(int i=total-1i>=0i--)
     {
      string name=GlobalVariableName(i);
      if(GetLastError()!=0)
        {
         PrintFormat("Error %d occurred while getting global variable name at index %d"GetLastError(), i);
         ResetLastError();
         continue;
        }
      PrintFormat("GlobalVariableName(%02d) = \"%s\""iname);
     }
//--- 清理干净
   GlobalVariablesDeleteAll(GV_NAME);
   /*
   结果:
   GlobalVariableName(20) = "TestGlobalVariableSet9"
   GlobalVariableName(19) = "TestGlobalVariableSet8"
   GlobalVariableName(18) = "TestGlobalVariableSet7"
   GlobalVariableName(17) = "TestGlobalVariableSet6"
   GlobalVariableName(16) = "TestGlobalVariableSet5"
   GlobalVariableName(15) = "TestGlobalVariableSet4"
   GlobalVariableName(14) = "TestGlobalVariableSet3"
   GlobalVariableName(13) = "TestGlobalVariableSet20"
   GlobalVariableName(12) = "TestGlobalVariableSet2"
   GlobalVariableName(11) = "TestGlobalVariableSet19"
   GlobalVariableName(10) = "TestGlobalVariableSet18"
   GlobalVariableName(09) = "TestGlobalVariableSet17"
   GlobalVariableName(08) = "TestGlobalVariableSet16"
   GlobalVariableName(07) = "TestGlobalVariableSet15"
   GlobalVariableName(06) = "TestGlobalVariableSet14"
   GlobalVariableName(05) = "TestGlobalVariableSet13"
   GlobalVariableName(04) = "TestGlobalVariableSet12"
   GlobalVariableName(03) = "TestGlobalVariableSet11"
   GlobalVariableName(02) = "TestGlobalVariableSet10"
   GlobalVariableName(01) = "TestGlobalVariableSet1"
   GlobalVariableName(00) = "TestGlobalVariableSet0"
   */
  }