我有函数,它返回数组:
Public Function BubbleSrt(ArrayIn, Ascending As Boolean)
Dim SrtTemp As Variant
Dim i As Long
Dim j As Long
If Ascending = True Then
For i = LBound(ArrayIn) To UBound(ArrayIn)
For j = i + 1 To UBound(ArrayIn)
If ArrayIn(i) > ArrayIn(j) Then
SrtTemp = ArrayIn(j)
ArrayIn(j) = ArrayIn(i)
ArrayIn(i) = SrtTemp
End If
Next j
Next i
Else
For i = LBound(ArrayIn) To UBound(ArrayIn)
For j = i + 1 To UBound(ArrayIn)
If ArrayIn(i) < ArrayIn(j) Then
SrtTemp = ArrayIn(j)
ArrayIn(j) = ArrayIn(i)
ArrayIn(i) = SrtTemp
End If
Next j
Next i
End If
BubbleSrt = ArrayIn
End Function
我尝试使用以下代码将返回数组分配给另一个数组:
sequence = BubbleSrt(unsorted, True)
声明:
Dim unsorted(2) As Integer
Dim sequence(2) As Integer
编译错误 - 无法分配给数组。解决方案可能非常简单,但我不知道该怎么做。
答案 0 :(得分:1)
改为使用这个:
Dim unsorted(2) As Integer
Dim sequence() As Integer
'initializing unsorted array
sequence = BubbleSrt(unsorted, True)
顺便问一下,您知道您的函数BubbleSrt
修改了您的unsorted
数组吗?例如。如果您在调用{3,1,2}
后使用值BubbleSrt
对其进行初始化,则unsorted
将变为&#34;排序&#34;:{1,2,3}
。如果您不想要它,请在函数声明中添加ByVal
:
Public Function BubbleSrt(ByVal ArrayIn, Ascending As Boolean)