Passing dynamic array into subroutine

Hi

I am passing a dynamic array into a subroutine as a variant and within the subroutine trying to add another element - for some reason I am getting a subscript out of range error. It is as though the passing of the array has “fixed” it ie it is no longer dynamic? To summarise the code:

Dim PCDAAbsWeeks() As String

Redim PCDAAbsWeeks(10)

'Set some values eg…

PCDAAbsWeeks(1)=“a value”

Then I pass it in…

Call OverPopulateAbsWeeks(projdoc.GetFirstItem(“StartDate”), key, PCDAAbsWeeks)

The routine…

Sub OverPopulateAbsWeeks(sdate As NotesItem, key As String, absarray As Variant)

Redim Preserve absarray(x) 'which equates to one more than the current ubound

At this point it fails!

Many thanks

Subject: Passing dynamic array into subroutine

Is the array declared globally? This may not work if it is a “local” variable.

You could always use a list instead, they are much nicer as you don’t have to worry about re-dimming. Though you will have some extra work to do to keep track of the index, but this shouldn’t be too hard.

HTH

Subject: RE: Passing dynamic array into subroutine

Thanks to everyone for the input. What I have now done is to replicate just the key bits of code in a separate agent. This seems to work perfectly (ie I can pass in the array as a variant and I can still redim it). so…

as has been suggested this must be to do with local/global declarations but I don’t see how this would make a difference - surely if I can see see the instantiated array in the CALLED routine in debugger it it should behave the same? One point to note - the array is declared globally in the agent, the agent calls the routine which sits in a script library.

In answer to what I am trying to do…the whole purpose of the routine is to add additional elements to the passed array (in cases where the original array - each element representing a week number - isn’t sufficient if the financial event falls after the final “week”).

Thanks again.

Subject: Passing dynamic array into subroutine

What exactly are you trying to accomplish? Since all parameters are passed by reference unless explicitly passed by value they actually reference the values that are passed through the parameter. The problem is arrays, lists, objects and user defined data types can not be passed by value, so you are in effect trying to redim a variable that is referenced outside the sub. I don’t know if you can change the reference variable in that way.

If you could tell us what you are trying to accomplish, we may be able to help suggest a better way of doing it.

brandt

Subject: Passing dynamic array into subroutine

Once the array is passed to the subroutine as a Variant, it is no longer considered an array (even though it just happens to be one).

A couple of options:

  1. Build a new instance of the array in your subroutine. Basically you’d dim a new dynamic array, run your input array through it and build it just like you built the first one. Once it’s built, you can then continue to treat it as a dynamic array and do whatever you want with it.

  2. If all you want to do is append an element to the end of the array, by far the easiest solution is to use ArrayAppend, i.e.,

MyArray = ArrayAppend(MyArray, “Some value”)

Although Help says the second param must also be an array, it will actually work with any scalar value (string, number, date, etc.). Only the first param must be an array.

Subject: Passing dynamic array into subroutine

I’m guessing, but I don’t think you can redim a variant… you are no longer dealing with an array at that point.