對不起,如果標題和問題不清楚;我沒有一個好的方式來描述它。但這裏是:混淆ParamArray行爲 - 重複數組以某種方式鏈接?
所以發生的事情是「testMat」以某種方式連接在一起,即使我沒有重新定義它們,它也會使值發生變化。例如,如果你在下面運行這個代碼,你會發現在testResult的matSum函數中,out1和out2的值正在改變(在循環中),我不知道爲什麼!它們的值在testResult1中不會改變。這種行爲從何而來?
Sub Main()
Dim testMat As Double(,) = {{1, 2}, {3, 4}}
Dim testResult As Double(,) = matSum(testMat, testMat, testMat)
Dim testResult1 As Double(,) = matSum({{1, 2}, {3, 4}}, {{1, 2}, {3, 4}}, {{1, 2}, {3, 4}})
End Sub
Function matSum(ByVal ParamArray args As Double()(,)) As Double(,)
'This function sums matrices. It assumes you know how to sum matrices.
Dim m, n As Integer
Dim out, out1, out2 As Double(,)
Dim numArgs As Integer = args.Length
out = args(0)
out1 = args(1)
out2 = args(2)
m = out.GetUpperBound(0)
n = out.GetUpperBound(1)
For v As Integer = 1 To numArgs - 1
For i As Integer = 0 To m
For j As Integer = 0 To n
out(i, j) = out(i, j) + args(v)(i, j)
Next
Next
Next
Return out
End Function
數組是一個引用類型,這就是它發生的原因。它與'ParamArray'無關。 – MarcinJuraszek 2014-11-03 03:20:22
對不起,你能詳細說一下嗎?我以爲我讀到ParamArray只是ByVal? (或者這不是你指的?) – Esteban 2014-11-03 03:25:13
'ByVal',你不能改變'args'的身份(例如,你不能讓'args'指向一個新的或不同的數組),但是你可以改變它的內容 - 即數組元素。 – 2014-11-03 03:33:16