Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im trying to return null if the textbox is empty, otherwise return the date, but it cant convert a datetime to null.
C#
public DateTime Date{
    get
    {
        if (calendarTextBox.Text == String.Empty)
        {
            return null;
        }
        else
        {
            return Convert.ToDateTime(calendarTextBox.Text);
        }
    }
    set
    {
        calendarTextBox.Text = value.ToShortDateString();
    }
}

thanks in advance!
Posted
Updated 16-Oct-13 13:47pm
v2

DateTime cannot be null. Other than making it a string I know of two options.

First option, use the MinValue (or MaxValue) date as sentinel value. Then have checks for the MinValue where needed.
C#
    return DateTime.MinValue; // set to MinValue
...
    // later in code check for MinValue
    if (Date != DateTime.MinValue)
    {
        // do something
    }

Option two, declare the DateTime as nullable. This will not be a DateTime object any longer and you will need to do type conversion when you map the nullable DateTime to a normal DateTime.
C#
// the ? declares this as a nullable DateTime, e.g. DateTime?
public DateTime? Date2
        {
            get
            {
                if (calendarTextBox.Text == String.Empty)
                {
                    return null;
                }
                else
                {
                    return Convert.ToDateTime(calendarTextBox.Text);
                }
            }
            set
            {
                // you must then convert the DateTime? type to a DateTime type
                calendarTextBox.Text = Convert.ToDateTime(value).ToShortDateString();
            }
        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 20:21pm    
Good answer, but I voted 4, as first variant is not strictly valid. "Minimum" time, strictly speaking, should not be considered as "null", "no time", it's some valid time.
Second option is much better, but... the whole OP's idea is not the best.
Please see Solution 2 for the alternative I advised.
—SA
idenizeni 17-Oct-13 20:11pm    
Thanks for the 4 and for the information.
PIEBALDconsult 16-Oct-13 22:03pm    
Don't use DateTime.MinValue - particularly if you want to store in SQL Server, Oracle, or another database that can't handle it.
idenizeni 17-Oct-13 20:12pm    
Good point.
Solution 1 is good enough, I would recommend the second option. However, using a text box for time is not a good idea. It's much better to use one of the DateTimePicker controls. For ASP.NET, it could be those found in my past answers:
TextBox allow only (HH:mm tt) format using asp.net[^],
DateTimePicker Web Control[^].

Good luck,
—SA
 
Share this answer
 
Comments
nebiam 16-Oct-13 20:36pm    
thanks for the reply, i use an asp calendar extender to populate the text box
Sergey Alexandrovich Kryukov 16-Oct-13 21:18pm    
Why?!
—SA
nebiam 16-Oct-13 22:06pm    
because i wanted a specific style calendar and couldnt achieve that using the generic datetimepicker
nebiam 16-Oct-13 22:12pm    
because i wanted a specific calendar style and couldnt achieve that using the generic datetimepicker.
nebiam 16-Oct-13 22:12pm    
because i wanted a specific calendar style and couldnt achieve that using the generic datetimepicker.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900