views:

46

answers:

2

I would like to loop through two lists using a For each loop.

dim data as list(of pointpairlist)
For each recLine in records
    For Each chan In recLine.channels and d in data
      d.add( func(chan) )
    Next
next

note: each record line has one sample from each channel recorded. ie each record line is a slice of a 32 sensor recordings. I want to build up a x,y list of data points for each channel (the x axis is common to all channels)

Is there some way to do it similar to what i have above (avoiding indexing variables)

+1  A: 

The best way to iterate over two different collections in a single loop is to use indexers.

For index As Integer = 0 To recLine.channels.Count - 1
    data(index) = func(chan(index))
Next

As @0xA3 comments, you can also a while loop, calling GetEnumerator, MoveNext and Current directly on each collection, but this buys you nothing.

I realize you want to avoid using indexers, but there is no simple language support for anything else.

This is the case also for C#.

Why do you need to avoid indexers?

Oded
Well, you *could* use a while loop and call `GetEnumerator` and `MoveNext` and `Current` yourself, if you really want to avoid an indexer. This should work well if the lists have the same size but it doesn't really give you an advantage.
0xA3
@0xA3 - thanks for the insight, answer updated.
Oded
well, it was a mix of trying to have less clutter, and knowing that I really don't know all the constructs in vb.net that well. I typically code in vb.net as if it was C or C++ which is probably not the most efficient use of the language.
michael
+1  A: 

Nest the loops. I cannot reverse-engineer the actual declarations from the snippet, but something like this:

    Dim data As List(Of PointPairList)
    ''...
    For Each point In data
        For Each chan In point.recLine
            func(chan)
        Next
    Next
Hans Passant
doesn't quite work because each "point" is actually a list of points populated by each channel (a points list for each channel list).But know i realize i left something out. I will edit the question
michael
Well, something like that. Seems you've adopted the nested loop approach. Why did you select an answer that doesn't use it?
Hans Passant
Cause thats what I ended up using! :)
michael