使用 A_Index 或 Variable 作为函数参数

Using A_Index or Variable as a function parameter

我搜索了internet/documentation,好像这不可能。但是我有大量的变量需要通过函数传递。功能正常。

Example of variable list:
st1mrks = 94
st2mrks = 34
st3mrks = ...

test1 = "student has collected " st1mrks " marks. "
test2 = "student has collected " st2mrks " marks. "
test3 = "student has collected " st3mrks " marks. "
test4 = ...
            Loop % totaltestnumber{

                    dofunction(test%A_Index%)

            }

或者,我尝试使用 while

i=0
while (i < totaltestnumber){
     dofunction(test%i%)
     i++
}

但这显然行不通。

有办法吗?

让我们看一些定义:

  • 函数接受参数
  • Parameters 必须始终完全定义
    • 即您必须提前指定 每个 参数。
    • 即如果你有 100 个变量 你必须定义 100 个参数
    • 这是因为参数成为函数内部局部变量的副本,因此需要提前完全定义。
  • Pseudo Arrays 不是数组。他们是"collection of sequentially numbered variables"。
    • 即您有 100 个独立变量
    • Pseudo Arrays虽然方便,但也不推荐使用.
  • Object-Based Arrays 另一方面 数组。它们是一个包含一系列东西的对象。
    • 即您有 1 个包含 100 个元素的变量

我看到 2 个选项:

选项 1:

如果您使用 Pseudo Arrays,并且有 100 个变量,那么您就不走运了。您必须将所有 100 个变量定义为需要单独传递的参数。没有简单的方法来动态地迭代它们。函数和参数不是那样工作的。

选项 2

更改为使用 Object-Based Arrays,而不是不同的变量。这样你只会传递一个对象。如果我们严格使用你的例子,你可以这样做:

Example of variable list:
st1mrks = 94
st2mrks = 34
st3mrks = ...

test1 = "student has collected " st1mrks " marks. "
test2 = "student has collected " st2mrks " marks. "
test3 = "student has collected " st3mrks " marks. "
test4 = ...


; Create the array, initially empty:
Array := []

Loop % totaltestnumber{
  Array.Push(test%A_Index%)
}

dofunction(Array)

....

dofunction(ByRef Array)
{
    Loop % Array.MaxIndex()
    {
      ...
    }
}

如果您从一开始就使用基于对象的数组,则可以进一步优化此代码。