Page 1 of 1

array loop problem (VB)

Posted: 22 May 2011, 01:03
by djwez
hey guys.
im having some trouble taking values from a 3 dimensional array and putting them into one string. for some reason my loop is only reading the first value (0,0,0) and not moving on to the second (0,0,1)
this is not all the code just the main looping bit

Code: Select all

        checkarray(0, 0, 0) = 2
        checkarray(0, 0, 2) = 3


        Dim str As String
        Dim x, y, z As Integer
        x = 0
        y = 0
        z = 0
        str = ""
        Do

            str = str & " "
            str = str & checkarray(x, y, z)
            Lbl_debug2.Text = z
            If z < 2 Then z += 1
            If z = 2 Then
                y += 1
                z = 0
            End If
            If y < 2 Then y += 1
            If y = 2 Then
                x += 1
                z = 0
                y = 0
            End If

            If x = 2 Then
                Exit Do
            End If

        Loop
        Lbl_debug.Text = str
thanks guys

Re: array loop problem (VB)

Posted: 22 May 2011, 09:50
by SykomantiS
My head hurts :|
Also I don't have VS at home, but it's pretty clear you aren't looping though all the values.
You are incrementing Y on two separate lines, which causes it to skip over. (There are probably other errors as well, but right now my brain refuses to show them to me)

At the end of each loop, your values for X, Y, Z are
0 0 0
0 1 1
1 0 0
1 1 1
2 0 0

At which time you exit the loop. Clearly, not all possible combinations are being checked (there should be 27 combinations to check- 3 values for each variable- 33)

I would use nested for loops, (for x as integer = 0 to 2) etc.


EDIT: Stupid typo...

Re: array loop problem (VB)

Posted: 22 May 2011, 10:31
by djwez
i had a feeling that was the case :)

thanks ill try it out