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
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
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.
Member
47 Points
367 Posts
Looping through Array items
Jan 19, 2014 02:12 PM|michaelcode|LINK
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.
All-Star
101931 Points
20703 Posts
Re: Looping through Array items
Jan 19, 2014 02:29 PM|MetalAsp.Net|LINK
All-Star
114593 Points
18503 Posts
MVP
Re: Looping through Array items
Jan 19, 2014 02:36 PM|Rion Williams|LINK
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 :
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 :
Member
47 Points
367 Posts
Re: Looping through Array items
Jan 20, 2014 12:29 PM|michaelcode|LINK
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!