For each...next

Hi

Quick one - in your Language Reference for For Each…Next, your example states that this:

Rem For Each...Next Example
Dim School, Employee
School = Array("Principal", "Mr. Garrison", "Chef")
For Each Employee In School
  Print "School employee:", Employee
Next

produces this:

School employee:     Principal
School employee:     Mr. Garrison
School employee:     Chef

However, what I’ve found (after having been driven nuts) is that it would actually produce this:

School employee:     0
School employee:     1
School employee:     2

and in order to get what I actually want, I would need to amend the code to this:

Print "School employee:", School[Employee]

Is the description wrong? Or has there been a change? Or have I found a bug or something?

Thanks.

I just copied and pasted the code to a project and ran it. I got this:

School employee:     Principal
School employee:     Mr. Garrison
School employee:     Chef

Really? Interesting. I’ll have to experiment a bit more to see why I’m getting the results I’m getting.

I’ll get back to you. Watch this space. :grin:

OK, yes, you’re correct, but it doesn’t work in the following scenario:

If I run the following code:

Select1.clear()
a=[""]
a[0]="Zero"
a[1]="One"
a[2]="Two"
For Each b in a
  Select1.addItem(b,b)
Next

I get the below dropdown.

The same thing happens for collections. If instead I’d used

a["Item1"]="Zero"
a["Item2"]="One"
a["Item3"]="Two"

the dropdown would show ‘Item1’, etc instead of ‘Zero’, etc, so to get the desired effect I have to use

Select1.addItem(a[b],a[b])

Is this expected behaviour?

And is there an easier way to populate a dropdown with an array?

Yes, this is what you need to do.