2

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!

6
  • That is because you are using arrInputData which is an array (a reference type) which gets overwritten with the next iteration. How about initializing arrInputData within the loop, same as what you are doing for arrOutputData? Commented Sep 13, 2015 at 19:46
  • Thanks! Can you please provide an example, because I need all n items of arrInputData to contain information, filled in reverse order, which will then be assigned to a specific item in arrInputArray. Thanks! Commented Sep 13, 2015 at 19:51
  • You can do arrInputData As Double() = New Double(intGlobalHistory) {} within the for loop, to start with. Commented Sep 13, 2015 at 20:00
  • Did what you said, initializing arrInputData within the intStepping loop and it worked magic. You are the man. Can't mark as the answer because it's a comment, though... Thanks! Commented Sep 14, 2015 at 0:12
  • In any case, bear in mind that arrInputArray(intStepping) = arrInputData is perhaps not doing exactly what you want. For example: if after this line you write arrInputArray(intStepping)(0) = 111.0 arrInputData(0) would also take the 111.0 value. That is: you are not copying the values from arrInputData into arrInputArray(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()). Commented Sep 14, 2015 at 7:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.