定义了一个接收变参数组的函数,如下。
function TForm1.Recieve(const Values: array of const): string;
beginend;问题是调用得是Recieve([1,2,3]);那如何把A: array of Integer的所有元素(setlength(a,3)),一次传递给RecieveRecieve([A])是不对的,Recieve(A)也不行。
请问除了Recieve([A[0],A[1],A[2]])外,有其他的能用写法吗?因为A是动态数组。

解决方案 »

  1.   

    In a function with an open array parameter, you can't call SetLength on the parameter. If you really only want to pass dynamic arrays, you'll have to declare them separately, and use the type name as parameter type.Although the syntax is unfortunately very similar, an open array parameter should not be confused with a Delphi dynamic array. A dynamic array is an array that is maintained by Delphi, and of which you can change the size using SetLength. It is declared like:  type
        TIntegerArray = array of Integer;Unfortunately, this looks a lot like the syntax used for open array parameters. But they are not the same. An open array parameter will accept dynamic arrays like array of Month, but also static arrays like array[0..11] of Month. 
    Hope it helps.//Ali
      

  2.   

    把A的地址传进去,例如Receive([@A])
      

  3.   


    我的意思是,A是个数组,比如我不想一个个把A的元素列在方括号内,比如Recieve([A[0],A[1],A[2]])。
    因为A的长度是动态的。 我希望能把数组A的所有元素一次传给Recieve。
    而不需要写成
    Recieve([A[0],A[1],A[2]],....)
      

  4.   


    我想说的是:
    type 
      TIntegerArray = array of Integer; function Receive(const Values : TIntegerArray);
    begin
         //可以用High和Low来循环
    end;procedure test;
    var
       A : TIntegerArray;
    begin
        SetLength(A,3);
        A[0] := 1;
        A[1] := 2;
        A[3] := 3;    Receive(A) //这个应该不会有任何问题
    end;
    Hope it helps.//Ali
      

  5.   

    array of const是通过压栈传递参数的
    针对楼主问题,较为省力的方法就是改变函数参数为动态数组,弃用开放数组PS:当一个函数参数声明为array of const,也就相当于它声明:我不关心你传来多少参数,也不限制某个参数的具体类型,你只要在调用我之前,把要传来的数据(或数据的地址)压栈就行了。