tags:

views:

420

answers:

4

i am new to vb.net and just wanted to clarify something. i have this code -

Dim i As Integer
For i = Now.Year To Now.Year

Next

for the code "For i = Now.Year To Now.Year", when the month changes to say May 2010, will the now.year to now.year change the 11 records to May 2011. or will it be may 2010 again?

+1  A: 

Now.Year only returns the year. So if you run it right now, it'll return 2010. If you run the same code next year, it'll return 2011.

Ok, let me try to do this with my poor VB skills. :)

Dim last As String
last = ""
Try
    Dim i As Integer
    Dim j As Integer
    Dim time As DateTime = DateTime.Now
    i = Now.Year
        For j = 11 To 0
            If j < (Now.Month - 1) Then
                i = (Now.AddYears(1).Year)
            Else
                i = Now.Year
            End If
            last = (time.AddMonths(j)).ToString("MMMM") + " " + (i.ToString)
            DDL.Items.Add(last)
        Next
End Try

If you run this, it will populate the drop down list with the remaining months left in this year, then increase your Year integer, and add the months next year up until this month next year.

At least I think this should work, but I'm not really up on my VB. It should give you an idea though.

James
ok let me give u the whole code Dim last As String last = "" Try Dim i As Integer Dim j As Integer Dim time As DateTime = DateTime.Now For i = Now.Year To Now.Year For j = 0 To 11 last = (time.AddMonths(j)).ToString("MMMM") + " " + (i.ToString) DDL.Items.Add(last) Next Nextthis code is for populating a dropdownlist. So now if i run this code in say april or may, how do i change the year for all the 11 records?
sdcsdc
+1  A: 

Based on your comment to this answer, try this:

Dim j As Integer
Dim time As DateTime = DateTime.Now
For j = 0 To 11
    Dim s As String = time.AddMonths(j).ToString("MMMM yyyy")
    DDL.Items.Add(s)
Next
Jason
A: 

I'm having a hard time understanding your question. 'Now' is a function which returns the current system time. If it is any month in 2010, Now.Year will return 2010. If it is any month in 2011, Now.Year will return 2011.

But, and this is important, the result of 'Now' can change from call to call. If it is new year's eve, you can call Now.Year once and get 2010 then call it again and get 2011. This could happen in your for loop, because it calls Now.Year twice! Even worse, 'Now' depends on the current time set by the user. If the user backs up the date by a year at just the wrong time your loop might not run at all! (ie. because for i = 2010 to 2009 will not execute the body once)

I see a loop that should be an assignment of the current year to the variable i. You probably mean something totally different.

Strilanc
A: 

Jason... perfect answer. thanks a lot

Dim j As Integer Dim time As DateTime = DateTime.Now For j = 0 To 11 Dim s As String = time.AddMonths(j).ToString("MMMM yyyy") DDL.Items.Add(s) Next

sdhcb
You're welcome. Welcome to StackOverflow! Be sure to check out the FAQ (http://stackoverflow.com/faq).
Jason