7 Replies - 2586 Views - Last Post: 16 March 2013 - 11:40 PM Rate Topic: -----

#1 danglesauce19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-February 12

Loop into List Box with value from 1-50 in reverse order

Posted 16 March 2013 - 04:15 PM

I need to fill an array into a list box initiated by a button click. Here is what it should look like in the list box

Ex:

Index is: 0 Value is: 50
Index is: 1 Value is: 49
Index is: 2 Value is: 48
etc.

Here is my code. Nothing seems to show up in the list box when I click the button.

Public Class Form2

    Private Sub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click
        Dim intArray(49) As Integer
        Dim i As Integer
        For i = 49 To 0
            intArray(i) = i - 1
            lstResult.Items.Add("Index is:" & i & " Value is: " & intArray(i))
        Next i
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Loop into List Box with value from 1-50 in reverse order

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Loop into List Box with value from 1-50 in reverse order

Posted 16 March 2013 - 04:22 PM

So I think this would an appropriate time, use your debugging skills.
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 369
  • View blog
  • Posts: 1,078
  • Joined: 01-February 13

Re: Loop into List Box with value from 1-50 in reverse order

Posted 16 March 2013 - 04:44 PM

When you want a For Next loop to count backwards it needs to have the Step - 1 like
For i = 49 To 0 Step -1

That should at least get things going. I think you may need to do some more work with displaying the index :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#4 danglesauce19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-February 12

Re: Loop into List Box with value from 1-50 in reverse order

Posted 16 March 2013 - 05:10 PM

This is what I have now. It outputs:

Index is: 50 Value is: 49
Index is: 50 Value is: 48
etc.

Here's my code:

Public Class Form2

    Private Sub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click
        Dim intArray(50) As Integer
        Dim i As Integer
        Dim j As Integer
        For i = 0 To 49
            i = i + 1
        Next
        For j = 50 To 1 Step -1
            intArray(j) = j - 1
            lstResult.Items.Add("Index is:" & i & " Value is: " & intArray(j))
        Next j
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Loop into List Box with value from 1-50 in reverse order

Posted 16 March 2013 - 05:22 PM

Do it with one loop, and no other variables excepts for the loop counter.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 369
  • View blog
  • Posts: 1,078
  • Joined: 01-February 13

Re: Loop into List Box with value from 1-50 in reverse order

Posted 16 March 2013 - 06:06 PM

Your intArray is zero indexed so that should be set to 49 which would mean it has 50 elements because it starts from 0. Then count 0 to 49 instead of 49 to 0 with the (For i) loop and inside the loop assign 50 - i to the intArray(i). Then use the same line of code to add it to the listbox. As AdamSpeight2008 said it should not need any other variables just the loop counter i to do it. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Loop into List Box with value from 1-50 in reverse order

Posted 16 March 2013 - 07:50 PM

View Postdanglesauce19, on 16 March 2013 - 06:10 PM, said:

This is what I have now. It outputs:

Index is: 50 Value is: 49
Index is: 50 Value is: 48
etc.

Allow me to point out a few things. Comments in your code.

Public Class Form2

    Private Sub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click
        Dim intArray(50) As Integer
        Dim i As Integer
        Dim j As Integer
   ' The following For loop really doesn't do anything at all. If you follow the link given by AdamSpeight2008,
   ' and follow the procedures in that tutorial on this code, it should be obvious what the loop is doing.
   ' All it's doing is to increment the loop variable (i), which is already being incremented
   ' because it's in a For loop. The result of that is that if you were using i to index something,
   ' you would be making it miss every second integer. It would go 0,2,4,6,8, etc.
        For i = 0 To 49
            i = i + 1     
        Next
   ' Step through the following loop to see what's happening. Check the values of i and j as you step through.
   ' You can also just read the code line by line to see what's happening. Use a pencil and paper to keep
   ' track of variables. It's just as effective to do it that way, just a lot slower.
        For j = 50 To 1 Step -1
            intArray(j) = j - 1
            lstResult.Items.Add("Index is:" & i & " Value is: " & intArray(j))
        Next j
    End Sub
End Class


[/quote]
Was This Post Helpful? 0
  • +
  • -

#8 danglesauce19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-February 12

Re: Loop into List Box with value from 1-50 in reverse order

Posted 16 March 2013 - 11:40 PM

I got it to work. Not the best idea to do homework while celebrating St Patrick's day haha
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1