Necuno
Court Jester
- Joined
- Sep 27, 2005
- Messages
- 58,567
expiryDateText is read as the text value of 1/12/2009
a)
DateTime? expiryDate = (string.IsNullOrEmpty(expiryDateText)) ? new DateTime?() : DateTime.Parse(expiryDateText);
b)
DateTime? expiryDate = (!string.IsNullOrEmpty(expiryDateText)) ? DateTime.Parse(expiryDateText) : new DateTime?();
c)
DateTime? expiryDate = (string.IsNullOrEmpty(expiryDateText)) ? DateTime.Now : DateTime.Parse(expiryDateText);
d)
now you would think that all 4 would yield the same result but no it doesn't. a and b always gives a null regardless if expiryDateText has a value or not. c and d however sets value as a parsed date from the string.
seems that the compiler/ide doesn't like it when new DateTime?() is used inline and goes null for some reason each time
a)
DateTime? expiryDate = (string.IsNullOrEmpty(expiryDateText)) ? new DateTime?() : DateTime.Parse(expiryDateText);
b)
DateTime? expiryDate = (!string.IsNullOrEmpty(expiryDateText)) ? DateTime.Parse(expiryDateText) : new DateTime?();
c)
DateTime? expiryDate = (string.IsNullOrEmpty(expiryDateText)) ? DateTime.Now : DateTime.Parse(expiryDateText);
d)
Code:
DateTime? expiryDate = null;
if(!string.IsNullOrEmpty(expiryDateText)) expiryDate = DateTime.Parse(expiryDateText);
now you would think that all 4 would yield the same result but no it doesn't. a and b always gives a null regardless if expiryDateText has a value or not. c and d however sets value as a parsed date from the string.
seems that the compiler/ide doesn't like it when new DateTime?() is used inline and goes null for some reason each time
Last edited: