本文介绍了将数组传递给ParamArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将数组的所有元素传递给ParamArray?

is it possible to pass all elements of an array to a ParamArray?

例如,我想将一个ParamArray传递给另一个ParamArray:

For example I'd like to pass a ParamArray to another ParamArray:

Sub test()
    p1 "test", "banane", "birne"
End Sub

Sub p1(ParamArray keys() As Variant)
    p2 keys 'should be the same as: p2 "test", "banane", "birne"
End Sub

Sub p2(ParamArray keys() As Variant)
    Dim key As Variant
    For Each key In keys
        Debug.Print key 'Run-time error '13' Type mismatch (key is an array)
    Next key
End Sub

在这种情况下,p2的ParamArray不包含keys的元素,但它获取数组对象keys.因此,我必须检查是否通过了数组:

In this case ParamArray of p2 doesn't contain the elements of keys, but it gets the array-object keys. Thus I've got to check, if an arrays is passed:

Sub test()
    p1 "test", "banane", "birne"
    p2 "test", "banane", "birne"
End Sub

Sub p1(ParamArray keys() As Variant)
    p2 keys
End Sub

Sub p2(ParamArray params() As Variant)
    Dim keys As Variant
    If IsArray(params(0)) Then
        keys = params(0)
    Else
        keys = params
    End If

    Dim key As Variant
    For Each key In keys
        Debug.Print key
    Next key
End Sub

但是与Java相比,这很尴尬:

But this is awkward for example compared to Java:

public class VarArgs {

    public static void main(String[] args) {
        p1("test", "banane", "birne");
        p2("test", "banane", "birne");

        String[] array = {"test", "banane", "birne"};
        p1(array);
        p2(array);
    }

    public static void p1(String... strings) {
        p2(strings);
    }

    public static void p2(String... strings) {
        for (String string : strings) {
            System.out.println(string);
        }
    }

}

在Java中,我不必区分.但这在VBA中可能是不可能的.

In Java I don't have to distinguish. But this is probably not possible in VBA.

感谢您的帮助,
迈克尔

Thanks for help,
Michael

推荐答案

将ParamArray参数传递给另一个需要ParamArray参数的函数(委托ParamArray参数).我需要委托给以下类型的函数:strf(str as string, ParamArray args() as Variant) as String在ParamArray中的其他函数中接收的参数直接传递而无需显式写入.我发现的限制是:

Pass a ParamArray argument to another function that expects a ParamArray argument (delegate ParamArray arguments).I need to delegate to a function of type: strf(str as string, ParamArray args() as Variant) as String the arguments received in other function in a ParamArray passing directly without explicitly write.The restrictions I've found are:

  1. 一个ParamArray()它只能传递给另一个需要ParamArray的函数.
  2. 在元素0处接收ParamArray作为Variant()
  3. 第二个功能接收到时,它会增加深度级别我没有找到任何令人满意的解决方案,但是我编写了一个功能完美的函数,撤消添加的深度级别并返回带有接收到的参数的向量.
  1. A ParamArray() it can only be passed to another function that expects a ParamArray.
  2. The ParamArray is received at element 0 as a Variant ()
  3. When the second function receives it increases a level of depthI have not found any satisfactory solution, but I have written a function that works perfectly, undoing the depth levels added and returning a vector with arguments received.

代码:

Option Explicit
Option Base 1

Public Sub PrAr1(ParamArray pa1() As Variant)
Dim arr() As Variant
  arr = fn.ParamArrayDelegated(pa1)
  PrAr2 pa1
End Sub

Public Sub PrAr2(ParamArray pa2() As Variant)
Dim i As Integer, arrPrms() As Variant
  arrPrms = fn.ParamArrayDelegated(pa2)
  For i = 0 To UBound(arrPrms)
    Debug.Print s.strf("i: %0 prm: %1 ", i, arrPrms(i))
  Next i
  PrAr3 pa2
End Sub

Public Sub PrAr3(ParamArray pa3() As Variant)
Dim i As Integer, arrPrms() As Variant
  arrPrms = fn.ParamArrayDelegated(pa3)
  For i = 0 To UBound(arrPrms)
    Debug.Print s.strf("i: %0 prm: %1 ", i, arrPrms(i))
  Next i
End Sub

Public Function ParamArrayDelegated(ParamArray prms() As Variant) As Variant
Dim arrPrms() As Variant, arrWrk() As Variant
'When prms(0) is Array, supposed is delegated from another function
  arrPrms = prms
  Do While VarType(arrPrms(0)) >= vbArray And UBound(arrPrms) < 1
    arrWrk = arrPrms(0)
    arrPrms = arrWrk
  Loop
  ParamArrayDelegated = arrPrms
End Function

这篇关于将数组传递给ParamArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 23:52