jimboweb
jimboweb

Reputation: 4542

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?

Upvotes: 0

Views: 979

Answers (4)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

Because DateTime is a value-type it can´t have a non-value. What you see in the debugger is thus only the default-value for it, however your if-statement is not executed. It is only the debugger showing anything for your variable because it can´t show null. Obviously your member Start is a DateTime whilst item.Start returns a DateTime?.

When you put a breakpoint into the statements within if you´ll notice it´ll never get executed thus. Only the debugger is creating this value, nothing is assigned to your variable - except its default value which apparently is the 1st of Jan. 0001 when defining your variable. So whereever you define newEvent, this is the line where this default-value is set - not within the if-statement.

Upvotes: 4

Nailbite
Nailbite

Reputation: 201

DateTime is a structure in C#; one consequence of this is its member variables are always initialized, in this case the value 1/1/0001 12:00:00 AM, which is also equal to DateTime.MinValue or default(DateTime). Another consequence is that structures are passed by value and cannot be null.

If you wish to have a DateTime "null" state, you might want to use Nullable<T> (or the shorthand ?) in your event class:

public class NewEvent {
    public DateTime? Start { get; set; }
    public DateTime? End { get; set; }
}

Nullable<T> gives you the HasValue property to check if the object has been assigned a value or not.

Upvotes: 0

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

That is the default value of DataTime type. You can use Nullable DataTime for your purpose.

DateTime? d = null;
bool boolNotSet = d.HasValue;

Upvotes: 1

Eli
Eli

Reputation: 693

It is skipping the code as you expect. But newEvent.Start has a value anyway, probably because it is of type DateTime, which cannot be null.

Upvotes: 1

Related Questions