2个集合循环同一时间

时间:2012-10-30 13:42:48

标签: vb.net collections

您正在使用结构和集合开始我的第一步,我使用相同的结构创建了2个集合。

一个集合保存今年和最后一个的数据。

是否有可能同时迭代两者?

Structure dateControl

   Dim StartDate As Date
   Dim EndDate As Date
   Dim weekCount As Integer
   Dim YearWeek As Integer

End Structure

Dim LastYearStruct As dateControl

    For I = 1 To howmanyweeks

        LastYearStruct.StartDate = DateAdd(DateInterval.WeekOfYear, I - 1, dateFirstSunday)
        LastYearStruct.EndDate = DateAdd(DateInterval.Day, 7, LastYearStruct.StartDate)
        LastYearStruct.weekCount = I
        LastYearStruct.YearWeek = I

        lastYear.Add(LastYearStruct)

    Next

Dim ThisYearStruct As dateControl

    For I = 1 To howmanyweeks

        ThisYearStruct.StartDate = DateAdd(DateInterval.WeekOfYear, I - 1, dateFirstSunday)
        ThisYearStruct.EndDate = DateAdd(DateInterval.Day, 7, ThisYearStruct.StartDate)
        ThisYearStruct.weekCount = I
        ThisYearStruct.YearWeek = I

        ThisYear.Add(ThisYearStruct)

    Next

我正在尝试像

这样的东西

今年和去年的每一件事

debug.print thing

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您可以同时手动迭代这些集合。

    Dim lastYearEnumerator = lastYear.GetEnumerator()
    Dim thisYearEnumerator = ThisYear.GetEnumerator()
    Do While lastYearEnumerator.MoveNext AndAlso thisYearEnumerator.MoveNext
        Dim lastYearThing = lastYearEnumerator.Current
        Dim thisYearThing = thisYearEnumerator.Current

        ' do stuff with things
    Loop

这将迭代两个集合,直到达到任一集合的末尾。

答案 1 :(得分:1)

只需使用For循环

For i As Integer = 1 To lastYear.Length
    Dim lastYearThing = lastYear(i)
    Dim thisYearThing = thisYear(i)
    ' ...
Next