I have the following code:
Dim arrInputData As Double() = New Double(intGlobalHistory) {}
Dim arrOutputData As Double() = New Double(0) {}
Dim arrInputArray As Double()() = New Double(intGlobalStepping)() {}
Dim arrOutputArray As Double()() = New Double(intGlobalStepping)() {}
For intStepping As Integer = 0 To intGlobalStepping
For intHistory As Integer = (intGlobalHistory + 1) To 1 Step -1
arrInputData(intHistory - 1) = dblSomeInputFactor + CDbl(intHistory)
Next intHistory
arrOutputData = New Double() {dblSomeOutputFactor + CDbl(intStepping)}
arrInputArray(intStepping) = arrInputData
arrOutputArray(intStepping) = arrOutputData
Next intStepping
However, when I assign a specific item in the arrInputArray to arrInputData:
arrInputArray(intStepping) = arrInputData
all other items in the arrInputArray variable also change to reflect the new value in the arrInputData variable.
What am I doing wrong? I only want the specific item in arrInputArray changed, leaving all other items untouched.
Thanks!
arrInputDatawhich is an array (a reference type) which gets overwritten with the next iteration. How about initializingarrInputDatawithin the loop, same as what you are doing forarrOutputData?arrInputData As Double() = New Double(intGlobalHistory) {}within the for loop, to start with.arrInputDatawithin theintSteppingloop and it worked magic. You are the man. Can't mark as the answer because it's a comment, though... Thanks!arrInputArray(intStepping) = arrInputDatais perhaps not doing exactly what you want. For example: if after this line you writearrInputArray(intStepping)(0) = 111.0arrInputData(0)would also take the 111.0 value. That is: you are not copying the values fromarrInputDataintoarrInputArray(intStepping), but linking both arrays (any modification in one of them will also be applied to the other one). To avoid this situation, you should either copy the values or assign a new instance of the array (e.g.,arrInputArray(0) = arrInputData.Clone()).