Home > Back-end >  Passing Lower and Upper Bound Array Dimensions as Arguments in VBA
Passing Lower and Upper Bound Array Dimensions as Arguments in VBA

Time:12-23

I'm wondering if its possible to pass both lower and upper bound array sizes (using the "To" keyword) via arguments. Ultimately, I would like to do something like this:

sub foo
    call bar(2 To 5)
end sub

sub bar(arrayDimensions)
    dim myArray() as long
    redim myArray(arrayDimensions)
end sub

But VBA throws a fit if I use the "To" keyword like this. Is there another easier alternative? Or am I doing something wrong? I know I could pass two arguments as a work around, but I would rather not do that if there's a better way. Thanks in advance for any help.

Edit to clarify why I would like to do this instead of using two arguments.

I made an array class that I can use to store my arrays in and easily modify them (eg. myArray.fill(0)). But I want the user interface with this array class to be the same as just using a plan old array.

sub foo
    dim regularArray() as long
    redim regularArray(5) 
    regularArray(3) = 250

    dim myArray as ArrayClass
    set myArray = factory.newArrayClass(5)
    myArray(3) = 250

end sub

This works great using default properties. My constructor is set up to either receive a one long argument to define the size of a 1D array. Or it can receive two long arguments to define the size of a 2D array. Or is can receive one range argument to build an array based on excel data. Or it can receive an array for its argument to instantiate with an array.

Right now my code works great as it is. But if I wanted to add a lower bound when I instantiate the class, lets say for a 2D array, then I have to add two more optional arguments, one for each dimensions.

That then leads to the question of: do two long arguments represent a 2D array, or does it represent the lower and upper bound of a 1D array. So it would get hairy in a hurry.

CodePudding user response:

Passing two arguments is not a a work around, it is the way to do what you want.

Sub foo()
    Call bar(2, 5)
End Sub

Sub bar(a As Long, b As Long)
Dim myArray() As Long
    ReDim myArray(a To b)
End Sub

This works equally well, I guess:

Sub foo2()
    Dim myArray(2 To 5) As Long
    Call bar2(myArray)
End Sub

Sub bar2(myArray() As Long)
    'Do stuff with myArray
End Sub

From a design point of view, it isn't clear why "foo" knows what dimensions are needed in "bar", and why "bar" can't set these dimensions (since "bar" is the function that is doing the work with "myArray" anyway). It is very unusual to see a construction like this. Also, for what its worth, I have never seen an array in practice that did not have a lower bound of either 0 or 1, so that is also a bit unusual.

CodePudding user response:

You are trying to make things easier for your users which is to your credit. Unfortunately, this can make the programmers life a bit more complicated.

There are two alternative strategies. 1 use multiple constructors. Its not a sin to have multiple methods to construct your array so you might have methods such as

factory.newArrayClassBySize(5)
factory.newArrayClassOneDim(5,10)
factory.newArrayClassTwoDim(5,10,3,22)
factory.newArrayClassByArray(InputArray)
factory.newArrayClassByRange(inputXlRange)

This is, by far ,is my preferred method as it makes it very explicit what the code is doing.

  1. An alternative strategy is to have 7 optional parameters where the name of the parameters makes it clear what the the constructor expects and will do. The limitation of this method is that users have to pay attention to the intellisense for the parameters e.g.
Public Function newArrayClass _
( _
    Optional Size as variant = Empty, _
    Optional Lbound1 as variant = Empty, _
    Optional Ubound1 as variant = Empty, _
    Optional Lbound2 as variant = Empty, _
    Optional Ubound2 as Variant = Empty, _
    Optional XlRange as variant = Empty, _
    Optional BasedOn as variant = Empty _
) as ArrayClass

Using the named parameters requires you todo more work to work out if a correct set of parameters has been provided.

  •  Tags:  
  • vba
  • Related