Arbeiten mit arrays in VBA

Bin ich ein bisschen ein problem, die Arbeit mit arrays in VBA, wo das gleiche wäre trivial in (fast) jeder anderen Sprache auch:

Public Function getArray() As MyType()
    'do a lot of work which returns an array of an unknown length'
    'this is O(N^2) or something equally intensive so I only want to call this once'
End Function

Public Sub doSomething()
    Dim myArray() As MyType
    Set myArray = getArray() 'FAILS with "Cannot assign to array error"'
End Sub

Ich denke, es könnte sein, dass ich zu definieren, die Länge des Arrays im Voraus, oder ReDim ein dynamisches array. Aber ich weiß nicht, die Länge des zurückgegebenen Arrays vor der Zeit, und ich möchte vermeiden, den Aufruf der Funktion zweimal:

Public Sub doSomething()
    Dim myArray(0 To UBound(getArray()) As MyType  'not entirely sure if this would work, but it involves calling getArray twice which I'd like to avoid
    Set myArray = getArray()
End Sub

In C# - oder Java-das äquivalent wäre:

public MyType[] getArray(){
    //do some work and return an array of an unknown length
}

public void doSomething(){
    MyType[] myArray;
    myArray = getArray();  //one line and I don't need to define the length of array beforehand
}
InformationsquelleAutor Iain Sproat | 2011-02-24
Schreibe einen Kommentar