Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incrementing an array in a loop 1

Status
Not open for further replies.

geneo300zx

Programmer
Jul 10, 2003
3
0
0
US
I need to loop through an array 7 times and place the last value in the array in the first position and move up all the other values. Any help with this would be greatly appreciated. For example:

myArray(0) = "test 0"
myArray(1) = "test 1"
myArray(2) = "test 2"
myArray(3) = "test 3"

first time through the loop
display test 0
display test 1
display test 2
display test 3

second time through the loop
display test 3
display test 0
display test 1
display test 2

third time through the loop
display test 2
display test 3
display test 0
display test 1

Fourth time through the loop
display test 1
display test 2
display test 3
display test 0

and so on.
 
To see how this works, create a form with a button and a list box control. Then, copy paste this code.

Code:
Private Sub Command1_Click()
    
    Dim myArray(0 To 3)
    Dim i As Long
    Dim j As Long
    
    myArray(0) = "test 0"
    myArray(1) = "test 1"
    myArray(2) = "test 2"
    myArray(3) = "test 3"
    
    List1.Clear

    For i = UBound(myArray) To LBound(myArray) Step -1
        For j = LBound(myArray) To UBound(myArray)
            List1.AddItem myArray((i + j + 1) Mod (UBound(myArray) + 1))
        Next
    Next
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top