Possible duplicate:
Enter the result with a conditional statement in C #
I was just working on a popup to change the user when I encountered the following problem when trying to change the user of Date of Death:
In this case, the property Deathfor the user is DateTime?(Nullable Date Time).
user.Death = (model.Death != null) ? DateTime.Parse(model.Death) : null;
So, I realized that I can just check if the value contained in the model (model.Death is a string) contains a value, I would set a date whose value, otherwise I would set it to zero, as shown above.
However, I was not able to use this syntax because it would not allow me to explicitly set user.Deathto null using the ternary operator, although it user.Death = nullworked just fine.
Solution Used:
Replace : nullwith: new Nullable<DateTime>()
I think I'm just wondering why I couldn't explicitly set the Nullable DateTime property to null using the ternary operator?
source
share