tags:

views:

212

answers:

3

Hello,

In vb.net I have two data values as shown below:

Dim startp as datetime
Dim endp as datetime

I have a function called ProcessData(soemdate) which processes dataporting.

In VB.net is there a way I can do something like

For each day between startp and endp
 ProcessData(soemdate)
Next

Thanks

+5  A: 
For Each Day As DateTime in Enumerable.Range(0, (endp - startp).Days) _
                            .Select(Function(i) startp.AddDays(i)) 
    ProcessData(Day)
Next Day
Joel Coehoorn
+1 for the enumerable.range approach (cool trick)-1 for being a bit obfuscated as to the purpose for whomever needs to support it.
JohnFx
Sorry for the delay on VB translation- something came up at work that took priority on my attention.
Joel Coehoorn
A: 

Yes, you can use an accumulator date:

Dim Accumulator as DateTime
Accumulator = startp

While (Accumulator <= endp)


    Accumulator = Accumulator.AddDays(1)
End While

Not tested, and I'm a C# programmer, so be easy if my syntax is wrong.

John Gietzen
+1  A: 

Here is another way to do this.

Dim startp As DateTime = New DateTime(2009, 1, 1)
Dim endp As DateTime = New DateTime(2009, 2, 1)
Dim CurrD As DateTime = startp

While (CurrD <= endp)        
    ProcessData(CurrD)
    Console.WriteLine(CurrD.ToShortDateString)
    CurrD = CurrD.AddDays(1)

End While
JohnFx