如果标题和问题不清楚,请见谅;我没有一个很好的方式来描述它。但它是这样的:
所以发生的事情是“testMat”以某种方式链接在一起,即使我没有重新定义它们,它也会使值发生变化。例如,如果你在下面运行这段代码,你会看到在 testResult 的 matSum 函数中,out1 和 out2 的值随着 out 的变化而变化(在循环中),我不知道为什么!它们的值在 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