locked
ArrayList indexes not set properly RRS feed

  • Question

  • User-1427396445 posted

    I have some basic code to fill items in an ArrayList as follows:

    Dim intSteps As ArrayList = New ArrayList()
    			
    Select Case Session("type")
    Case 1
    	intSteps.Add(0)
    	intSteps.Add(1)
    	intSteps.Add(2)
    	intSteps.Add(6)
    	intSteps.Add(11)
    	intSteps.Add(12)
    Case 2
    	intSteps.Add(0)
    	intSteps.Add(1)
    	intSteps.Add(2)
    	intSteps.Add(5)
    	intSteps.Add(6)
    	intSteps.Add(11)
    	intSteps.Add(12)
    Case Else
    	intSteps.Add(0)
    	intSteps.Add(1)
    	intSteps.Add(2)
    	intSteps.Add(3)
    	intSteps.Add(4)
    	intSteps.Add(5)
    	intSteps.Add(6)
    	intSteps.Add(7)
    	intSteps.Add(8)
    	intSteps.Add(9)
    	intSteps.Add(10)
    	intSteps.Add(11)
    	intSteps.Add(12)
    End Select
    
    Dim j As Integer
    	For j = 0 To intSteps.Count - 1
            Response.Write(intSteps.IndexOf(j) & " : " & intSteps.Item(j) & "<br />")
    Next

    The issue is that for Case 1 the output is:

    0 : 0
    1 : 1
    2 : 2
    -1 : 6
    -1 : 11
    -1 : 12

    For Case 2, it's:

    0 : 0
    1 : 1
    2 : 2
    -1 : 5
    -1 : 6
    3 : 11
    4 : 12

    And for Else, it's:

    0 : 0
    1 : 1
    2 : 2
    3 : 3
    4 : 4
    5 : 5
    6 : 6
    7 : 7
    8 : 8
    9 : 9
    10 : 10
    11 : 11
    12 : 12

    Why is the index being set as -1 for a few items?

    Tuesday, August 24, 2010 11:44 PM

Answers

  • User1224194097 posted

    you should access the element by Index not Index of

    For j = 0 To intSteps.Count - 1
                Response.Write(j & " : " & intSteps(j) & "<br />")
            Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 25, 2010 12:27 AM

All replies

  • User-1427396445 posted

    No need to worry, figured out what I was doing wrong Laughing

    Wednesday, August 25, 2010 12:22 AM
  • User1224194097 posted

    you should access the element by Index not Index of

    For j = 0 To intSteps.Count - 1
                Response.Write(j & " : " & intSteps(j) & "<br />")
            Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 25, 2010 12:27 AM