locked
Looping through Array items RRS feed

  • Question

  • User-1941296693 posted

    I'm trying to loop through an array with asp.net & vb.net.

    The array hold two values for each item in the array.

    I know this code is wrong, any ideas on how to do it.

    Dim arr As New ArrayList

    arr.Add("1", "2")
    arr.Add("2", "5")
    arr.Add("3", "9")

    For i As Integer = 0 To arr.Count - 1

    Dim value1 As String = arr(i).ToString()
    HttpContext.Current.Response.Write(value1 & "<br />")

    Dim value2 As String = arr(i).ToString()
    HttpContext.Current.Response.Write(value2 & "<br />")

    Next

    any help?

    thanks.

    Sunday, January 19, 2014 2:12 PM

Answers

  • User281315223 posted

    If you are only concerned with the second set of value ("2","5","9") then and your other values are simply indices, you could just add those values and iterate through them as you might expect.

    If you need to store two values per item, then you have a few choices which I'll detail below.

    Store Arrays within your ArrayList

    You could consider storing an array with two values in each index of your ArrayList so that when you iterate through the list, you could access both the values by their indices :

    'Build your Dictionary'
    Dim arr As New ArrayList()
    
    'Add your values'
    arr.Add(New String() {"1", "2"})
    arr.Add(New String() {"1", "5"})
    arr.Add(New String() {"3", "9"})
    
    'Iterate through your Dictionary values'
    For i As Integer = 0 To arr.Count - 1
          'Grab the key at this index'
          Dim value1 As String = arr(i)(0)
          HttpContext.Current.Response.Write(value1 & "<br />")
          'Grab the value at this index'
          Dim value2 As String = arr(i)(1)
          HttpContext.Current.Response.Write(value2 * "<br />")
    Next

    Use A Dictionary 

    A Dictionary stores values a Key Value Pairs meaning that each Key (your first value) can coorepond to one or more other values (your second value) and it would function basically the same way :

    'Build your Dictionary'
    Dim arr As New Dictionary(Of String, String)
    
    'Add your values'
    arr.Add("1", "2")
    arr.Add("2", "5")
    arr.Add("3", "9")
    
    'Iterate through your Dictionary values'
    For Each pair As KeyValuePair(Of String, String) In arr
          'Grab the key at this index'
          Dim value1 As String = pair.Key
          HttpContext.Current.Response.Write(value1 & "<br />")
          'Grab the value at this index'
          Dim value2 As String = pair.Value
          HttpContext.Current.Response.Write(value2 & "<br />")
    Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 19, 2014 2:36 PM

All replies

  • User-1360095595 posted
    Not a vb person but I think it would be something like: ar(i)(j) where j is another loop within the first loop.
    Sunday, January 19, 2014 2:29 PM
  • User281315223 posted

    If you are only concerned with the second set of value ("2","5","9") then and your other values are simply indices, you could just add those values and iterate through them as you might expect.

    If you need to store two values per item, then you have a few choices which I'll detail below.

    Store Arrays within your ArrayList

    You could consider storing an array with two values in each index of your ArrayList so that when you iterate through the list, you could access both the values by their indices :

    'Build your Dictionary'
    Dim arr As New ArrayList()
    
    'Add your values'
    arr.Add(New String() {"1", "2"})
    arr.Add(New String() {"1", "5"})
    arr.Add(New String() {"3", "9"})
    
    'Iterate through your Dictionary values'
    For i As Integer = 0 To arr.Count - 1
          'Grab the key at this index'
          Dim value1 As String = arr(i)(0)
          HttpContext.Current.Response.Write(value1 & "<br />")
          'Grab the value at this index'
          Dim value2 As String = arr(i)(1)
          HttpContext.Current.Response.Write(value2 * "<br />")
    Next

    Use A Dictionary 

    A Dictionary stores values a Key Value Pairs meaning that each Key (your first value) can coorepond to one or more other values (your second value) and it would function basically the same way :

    'Build your Dictionary'
    Dim arr As New Dictionary(Of String, String)
    
    'Add your values'
    arr.Add("1", "2")
    arr.Add("2", "5")
    arr.Add("3", "9")
    
    'Iterate through your Dictionary values'
    For Each pair As KeyValuePair(Of String, String) In arr
          'Grab the key at this index'
          Dim value1 As String = pair.Key
          HttpContext.Current.Response.Write(value1 & "<br />")
          'Grab the value at this index'
          Dim value2 As String = pair.Value
          HttpContext.Current.Response.Write(value2 & "<br />")
    Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 19, 2014 2:36 PM
  • User-1941296693 posted

    Hi, I'd just like to thank you all for your replies. Sometimes it's taken for granted and people, no doubt myself included, don't take the time to thank you for your answers.

    Thank you!

    Monday, January 20, 2014 12:29 PM