0

Newby here.

I want to assign an array to an array of arrays (or jagged array).

I am doing the following:

    Dim arrOutputData As Double() = New Double(0) {}
    Dim arrOutputArray As Double()() = New Double(9)() {}

    For intStepping As Integer = 0 To 9
        arrOutputData(0) = intStepping
        arrOutputArray(intStepping) = arrOutputData
    Next intStepping

What I want is that arrOutputArray(0)(0)=0 on the first iteration, then arrOutputArray(1)(0)=1 on the second iteration, arrayOutputArray(2)(0)=2 and so forth.

What's actually happening is that arrOutputArray(0)(0)=0 on the first iteration, but the instant arrOutputData(0) is assigned 1 on the second iteration, arrOutputArray(0)(0) now equals 1 without even arriving at the line where arrOutputArray(1) = arrOutputData.

Tried this on vb.net 2008 and 2015 (on different computers) and I get the same result.

What am I doing wrong???

What bewilders me is that when the code reaches and executes arrOutputData(0)=1, immediately arrOutputArray(0)(0)=1. When the code reaches and executes the next line arrOutputArray(1)=arrOutputData, both arrOutputArray(0)(0) and arrOutputArray(1)(0) are now equal to 1.

Tried changing declaration syntax so Redim Preserve works, and still the same behaviour...

What gives?

Thanks!

Edit: I tried new 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

While now the arrOutputData and arrOutputArray variables work great, the arrInputData and arrInputArray don't, in the sense that it's behaving like described in the first part of the question. Any ideas?

I feel like I must change something here to include the New keyword somehow:

   arrInputData(intHistory - 1) = dblSomeInputFactor + CDbl(intHistory)

just not sure how or where exactly...

1
  • Where does intGlobalHistory, intGlobalStepping, dblSomeInputFactor, dblSomeOutputFactor come from and what are their initial values? Commented Sep 13, 2015 at 18:50

1 Answer 1

1

You need to change this

    arrOutputData(0) = intStepping

to this

    arrOutputData = New Double() {intStepping}

or more compressed like this (skipping the "arrOutputData" variable)

    arrOutputArray(intStepping) = New Double() {intStepping}

Instead of looping one can also assign values at creation time like this

    Dim arrOutputArray As Double()() = New Double()() {New Double() {1, 2, 3}, New Double() {4, 5, 6}, New Double() {7, 8, 9}}

which will give you three items in the arrOutputArray, each holding a three
item array

You can also assign a value array to a specific index of the arrOutputArray like this

    arrOutputArray(3) = New Double() {22, 33, 44}
    or
    arrOutputData = New Double() {22, 33, 44}
    arrOutputArray(3) = arrOutputData

just make sure the index exist or an exception will occur.

Check this link: The difference between value types and reference types

Sign up to request clarification or add additional context in comments.

5 Comments

Works beautifully. This works well for filling the array in a sequential manner, from 0 to 9. How do I go about assigning a direct index, for example 3, as in: arrOutputData(3) = intStepping if the arrOutputData array were initialized to 3 or more indexes? Thanks!
Hello, your answer did solve my main question; marked accordingly. I will try the more compressed form as you have illustrated. However, if I do not wish to skip the arrOutputData, but instead assign the arrOutputData variable to a specific index of arrOutputArray, how would I go about doing that?
Updated my answer with a sample of direct assigning of a value array to a specific index.
Thank you very much! That worked, however, I updated my question to now reflect Input arrays, which don't work. Any help appreciated.
Thanks for the heads up. New question here: stackoverflow.com/questions/32553814/…

Your Answer

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