locked
Condition Operators (If and Else statement) RRS feed

  • Question

  • User119289251 posted

    Hi all, I’m learning VB trying to develop a very simple holiday system.

    I am working with 2 calendars, so that u can select a start date and end date for a holiday year. But the holiday year starts in December and ends in November of the next year. I wrote a code for it so that when a person selects a start date, and chooses an end date in December, it stops them from adding it with a message, but it’s not giving me what I want.

    This is it,

     

    If Not Year(CalendarStart.SelectedDate) = Year(CalendarEnd.SelectedDate) Or _

            (Not (Year(CalendarEnd.SelectedDate) = Year(CalendarStart.SelectedDate) - 1 And Month(CalendarEnd.SelectedDate) = 12)) Then

                MessageLabel.Text = "Holiday Year ends in November"

                Exit Sub

            End If

     

    At present, it is stopping me from adding any holiday, PLEASE what is wrong with the statement?

     

    Friday, February 12, 2010 11:37 AM

Answers

  • User-952121411 posted

    Give the following code a try; I believe it covers every combintation and the results should be as expected:

            If (Year(CalendarEnd.SelectedDate) > Year(CalendarStart.SelectedDate)) AndAlso ((Month(CalendarEnd.SelectedDate) = 12)) _
                Or ((Month(CalendarStart.SelectedDate) <= 11) And (Year(CalendarEnd.SelectedDate) <> Year(CalendarStart.SelectedDate))) _
                Or ((Month(Now) < 12 And (Month(CalendarStart.SelectedDate) <> 12)) AndAlso (Month(CalendarEnd.SelectedDate) = 12)) Then
    
                MessageLabel.Text = "Holiday Year ends in November"
                Exit Sub
    
            End If


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 17, 2010 9:51 AM

All replies

  • User-1567259439 posted

    Try like this:

    If Year(CalendarStart.SelectedDate) <> Year(CalendarEnd.SelectedDate)


    If it is still not working, try to output your conditions.  Like This:

    If Year(CalendarStart.SelectedDate) <> Year(CalendarEnd.SelectedDate)

       MessageLabel.Text = "CalendarStart=" + Year(CalendarStart.SelectedDate)

    Else

       MessageLabel.Text = "CalendarEnd=" + Year(CalendarEnd.SelectedDate)

    End If

    Friday, February 12, 2010 11:48 AM
  • User119289251 posted

    @Illinoistin  Just tried it, it didnt work 

    Friday, February 12, 2010 11:56 AM
  • User-1567259439 posted

    What was the output of the textbox?  You need to have valid variables to compare.

    Friday, February 12, 2010 12:02 PM
  • User-1840559507 posted

    If

    (Year(CalendarStart.SelectedDate) < Year(CalendarEnd.SelectedDate))

    And

    Month(CalendarEnd.SelectedDate) = 12 Then

                MessageLabel.Text = "Holiday Year ends in November"

                Exit Sub

            End If

    Sunday, February 14, 2010 1:10 AM
  • User-952121411 posted

    There are (2) conditions you need to check:  is someone picking 'December' from the next year or is someone picking December in the current year and it is not yet December.  Try the code below:

            'The 'AndAlso' operator will not test the second condition if the 1st one does not evalute to 'True'
            'The condition in the 'OR' also makes sure that someone in the current year is not slecting a date in December, and it is not yet December
            If (Year(CalendarEnd.SelectedDate) > Year(CalendarStart.SelectedDate)) AndAlso (Month(CalendarEnd.SelectedDate) = 12) _
                Or ((Month(Now) < 12) AndAlso (Month(CalendarEnd.SelectedDate) = 12)) Then
    
                MessageLabel.Text = "Holiday Year ends in November"
                Exit Sub
    
            End If

     

    Hope this helps! Smile

    Monday, February 15, 2010 11:15 AM
  • User119289251 posted

    Thanks so much atconway, it works, but there is now a small problem.

    It allows me select December as a Start date and any other month for the end date. But when the start and end date are both in December, it doesnt work.

     

    Looking 4ward to ur reply

     

    Wednesday, February 17, 2010 9:16 AM
  • User-952121411 posted

    Give the following code a try; I believe it covers every combintation and the results should be as expected:

            If (Year(CalendarEnd.SelectedDate) > Year(CalendarStart.SelectedDate)) AndAlso ((Month(CalendarEnd.SelectedDate) = 12)) _
                Or ((Month(CalendarStart.SelectedDate) <= 11) And (Year(CalendarEnd.SelectedDate) <> Year(CalendarStart.SelectedDate))) _
                Or ((Month(Now) < 12 And (Month(CalendarStart.SelectedDate) <> 12)) AndAlso (Month(CalendarEnd.SelectedDate) = 12)) Then
    
                MessageLabel.Text = "Holiday Year ends in November"
                Exit Sub
    
            End If


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 17, 2010 9:51 AM
  • User119289251 posted

    Thank u so so much, it works 

    Wednesday, February 17, 2010 10:10 AM