lundi 27 juin 2016

c# If statement block executing even when false

So I am getting event data from Google Calendar event. I have a variable item that is of class Google.Apis.Calendar.v3.Data.Event. There is a property called Start, which is when the even starts. If it's set at a certain time of day, then Start.DateTime will have a value. If it's an all-day event, then Start.DateTime will be null, and it will have a separate property Start.Date which is a string for some reason. If Start.DateTime doesn't have a value, I want to get the Start.Date value insead. Here's the relevant code:

if (item.Start.DateTime.HasValue)
                {
                    newEvent.Start = (DateTime)item.Start.DateTime;
                    newEvent.End = (DateTime)item.End.DateTime;
                } else
                {
                    string StartDay = item.Start.Date;
                    string EndDay = item.End.Date;
                }

So when I run it on a date with a regular DateTime, I see this:

DateTime.HasValue is true

And it assigns a value to NewEvent.Start as it should. Great.

Now I run it with an all-day event where Start.DateTime is null:

DateTime.HasValue is false

So it should skip to the else statement, but it doesn't:

Start still gets a value

The newEvent.Start is still assigned a value, even though the condition is false. And the else statement isn't executed. What's going on?

Aucun commentaire:

Enregistrer un commentaire