Hi All,
Apologies in advance for the code dropping I'm about to do, but it should make testing easier. In VBA I have the following procedure to test sorting procedures:
I am using this to call the following procedure, which in turn calls a bubble sort procedure:
The above chain of events does not give the correct output, however using the BubbleSort procedure as:
does. Which means I am enquiring as to how one passes a ParamArray for modification of the arrays.
I found a post online saying I could redefine my BubbleSort procedure with the variant, as above, and it also suggested I could simply use Call BubbleSort(Ascending, Arr) although this gave me an error: "Invalid ParamArray use" which on investigating the help file indicates I should "Assign the ParamArray parameter to a Variant, and then pass the variant" which I how I obtained the above code.
? statements in the Intermediate window indicate that the sorting procedure works and that, right before exiting the BubbleSortWrapper procedure, the ParamArray Arr has the correct values sorted, however immediately after exiting the sorting procedure the values are unchanged, leading me to believe this is some issue with ByRef ByVal.
Solution?
Apologies in advance for the code dropping I'm about to do, but it should make testing easier. In VBA I have the following procedure to test sorting procedures:
Code:
Sub TestSort()
Dim Names(1 To 10) As String
Dim Order(1 To 10) As Integer
Dim RndNos(1 To 10) As Single
Dim i As Integer
Randomize
For i = 1 To 10
Order(i) = i
RndNos(i) = Rnd()
Next i
Names(1) = "Alice": Names(2) = "Bob": Names(3) = "Cathy"
Names(4) = "Doofus": Names(5) = "Ed": Names(6) = "Fergy"
Names(7) = "Grant": Names(8) = "Helena": Names(9) = "Ira": Names(10) = "Jacquie"
Call BubbleSortWrapper(True, RndNos, Names, Order)
Debug.Print vbCrLf & "Random Numbers Sorted"
For i = 1 To 10
Debug.Print RndNos(i)
Next i
Call BubbleSortWrapper(True, Order, Names, RndNos)
Debug.Print vbCrLf & "Names Sorted"
For i = 1 To 10
Debug.Print Names(i)
Next i
End Sub
I am using this to call the following procedure, which in turn calls a bubble sort procedure:
Code:
Sub BubbleSortWrapper(Ascending As Boolean, ParamArray Arr() As Variant)
Dim tmpArr As Variant
tmpArr = Arr
Call BubbleSort(Ascending, tmpArr)
Arr = tmpArr
End Sub
Code:
Sub BubbleSort(Ascending As Boolean, Arr As Variant)
' Sorts arrays Arr based on Arr1 in Asc order using the bubble sort algorithm
Dim tmpArr() As Variant
Dim i As Integer, j As Integer, k As Integer
On Error Resume Next ' Handle optional arrays
ReDim tmpArr(0 To UBound(Arr)) ' One temporary storage element for each array
For i = LBound(Arr(0)) To UBound(Arr(0))
For j = 1 To UBound(Arr(0)) - i
If IIf(Ascending, Arr(0)(j + 1) < Arr(0)(j), Arr(0)(j + 1) > Arr(0)(j)) Then
For k = LBound(Arr) To UBound(Arr)
tmpArr(k) = Arr(k)(j + 1)
Arr(k)(j + 1) = Arr(k)(j)
Arr(k)(j) = tmpArr(k)
Next k
End If
Next j
Next i
End Sub
The above chain of events does not give the correct output, however using the BubbleSort procedure as:
Code:
Sub BubbleSort(Ascending As Boolean, ParamArray Arr() As Variant)
I found a post online saying I could redefine my BubbleSort procedure with the variant, as above, and it also suggested I could simply use Call BubbleSort(Ascending, Arr) although this gave me an error: "Invalid ParamArray use" which on investigating the help file indicates I should "Assign the ParamArray parameter to a Variant, and then pass the variant" which I how I obtained the above code.
? statements in the Intermediate window indicate that the sorting procedure works and that, right before exiting the BubbleSortWrapper procedure, the ParamArray Arr has the correct values sorted, however immediately after exiting the sorting procedure the values are unchanged, leading me to believe this is some issue with ByRef ByVal.
Solution?