venturoso venturoso - 3 months ago 11
Vb.net Question

vb.Net - Looping through array returns error: expression of type which is not a collection type

I am trying to get the index value of an array by using a for Each loop.
I am making an error in my code that I do not know how to correct.

I cannot loop through the ArrayList because it is not a number.

Dim num As Integer = 0
If Not IsNothing(BreadCrumbIDs) Then
For Each num In BreadCrumbIDs
If num >= 0 And num <= 1 Then
Console.WriteLine(num)
Else
' Do nothing
End If
Next
End If


I would like to loop throught the arrylist and obtain the index/ numeric values of it.

Let's say BreadCrumbsIds contains:

0 - shoes
1 - boots
2 - leather boots

I want to do something when the values are between 0 and 1 and something else when value is 2.

The Array List property code:

Public Property BreadCrumbIDs() As ArrayList
Get
Return bcIDs
End Get
Set(ByVal value As ArrayList)
bcIDs = value
End Set
End Property

Answer

Instead of looping the items in the list, loop the index.

For index As Integer = 0 To BreadCrumbIDs.Count-1

Then you can access the items with

BreadCrumbIDs(index)

If BreadCrumbIDs isn't a list of numbers, then I would suggest you change its name.

An other option could be to use the IndexOf method of the list.