0

This is my first time asking in Stack Overflow :)

I have a program that lets the user pick a date in a combobox and display it on the label. The other parts of the code for that is fine but I cant make exception for months with special days (like february which only has 29 days in leap years). I tried using the If/Else statement so that when the users click the years 2016,2012,2008 and 2004 and february it will display on the combobox named "day" 1 to 29 and if the users click february but not the specified dates it will display 1 to 28:

If month.SelectedItem = "Feb" And year.SelectedItem = "2016" Or "2012" Or "2008" Or "2004" Then
        Dim jkl As Integer
        For jkl = 1 To 29
            day.Items.Add(jkl)
        Next
    ElseIf month.SelectedItem = "Feb" Then
        Dim poi As Integer
        For poi = 1 To 28
            day.Items.Add("poi")
        Next
    End If

But unfortunately when i debug it, when i select dates other than the specified in the first If statement the combo box named day only displays 29 instead of 28. I tried changing the order of the conditions, changing the separator of the numbers to "&" but it is still the same.

I hope someone gets to the bottom of this. I feel like its in my structure that is wrong but even if I change the order everytime and it is still displaying the same bug. I tried searching here but I cant find one like my condition even remotely similar.

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58

3 Answers3

4

Turn Option Strict On, it will help you detect these errors right away. VB does not handle If statement like this, what you wrote doesn't do what you think it does. You need to specify both side every time.

If month.SelectedItem = "Feb" And (year.SelectedItem = "2016" Or year.SelectedItem = "2012" Or year.SelectedItem = "2008" Or year.SelectedItem = "2004") Then

As for your logic, there are formula to detect leap day. It might be better to use it instead.

Lastly, near the end, you add the string and not the variable value.

day.Items.Add(poi)
the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • 3
    I would add that `AndAlso` & `OrElse` should be used in this case instead of `And` & `Or`. The OP can read [this](https://stackoverflow.com/a/302067/4934172) for more info. – 41686d6564 stands w. Palestine Nov 22 '18 at 15:55
  • 1
    How about `Date.IsLeapYear(2004)` – Mary Nov 23 '18 at 03:11
  • Where can i find the option strict. Is that an option or code needed to be written. Thanks Ahmed :) – diabetes_comms Nov 23 '18 at 07:30
  • @diabetes_comms You can either write `Option Strict On`/`Off` on top of a file to enable/disable it _only_ for that file or you can apply it to the whole project from the project properties. Another way _(which is the one you should do, if you ask me)_ is to turn Option Strict On in the Visual Studio settings so that it's enabled for all your upcoming projects. Check [this question](https://stackoverflow.com/q/2454552/4934172) for guidance and more info. Hint: Read both the first and the second answers. – 41686d6564 stands w. Palestine Nov 24 '18 at 13:28
4

I think you should use here the DateTime functions:

DateTime.DaysInMonth(year, month)

Docs

        Dim dateString As String = String.Format("{0} 1 2001", month.SelectedItem)
        Dim dDate As Date = DateTime.Parse(dateString)
        Dim numberOfMonth As Integer = DateAndTime.Month(dDate)
        For jkl = 1 To DateTime.DaysInMonth(numberOfMonth, year.SelectedItem)
            day.Items.Add(jkl)
        Next
-1

Personally, i would have made a nested If

if month.SelectedItem = "Feb" then
if year.SelectedItem = "2016" Or "2012" Or "2008" Or "2004" Then
Dim jkl As Integer
    For jkl = 1 To 29
        day.Items.Add(jkl)
    Next
else
Dim poi As Integer
    For poi = 1 To 28
        day.Items.Add("poi")
    Next
end if
ThisIsMe
  • 274
  • 1
  • 5