How to initialize an empty datetime variable?

I try to check if the variables DateTime "starttime"and "endtime"values ​​have null, and then try to initialize the value emptyas shown below, but I encountered below compilation errors. What is the best way to achieve this?

string htmllink = "";
DateTime? starttime = null;
DateTime? endtime = null;
htmllink = (dbNullCheck.isColumnNull(rdr, "html_link")) ? "" : rdr.GetString(3);
starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) ? "" : rdr.GetString(4);
endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) ? "" : rdr.GetString(5);

results.htmllist.Add(new gethtmllist() { resulthtmllink = htmllink, 
    duration = (starttime - endtime).ToString() });

Mistake:

Error 2: Cannot implicitly convert type 'string' to 'System.DateTime?'

UPDATE: -

                string htmllink = "";
                htmllink = (dbNullCheck.isColumnNull(rdr, "html_link")) ? "" : rdr.GetString(3);                    
                DateTime? starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) ? new DateTime() : rdr.GetDateTime(4);
                DateTime?  endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) ? new DateTime() : rdr.GetDateTime(5);

               results.htmllist.Add(new gethtmllist()
                {
                    resulthtmllink = htmllink,
                    duration = starttime.HasValue && endtime.HasValue ? (endtime.Value - starttime.Value).ToString() : ""
                });
+6
source share
4 answers

I am trying to execute string.Empty to check: starttime = string.IsNullOrEmpty(str) ? (DateTime?)null : DateTime.Now;

Successfully. So your code could be:

starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) ? (DateTime?)null : rdr.GetDateTime(4);
    endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) ? (DateTime?)null: rdr.GetDateTime(5);

Update 1: .Net Sample script for string.Empty: Link

+1
source

, starttime endtime , DateTime?. , , DateTime? .

DateTime? starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) 
    ? null 
    : rdr.GetDateTime(4);

DateTime? endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) 
    ? null 
    : rdr.GetDateTime(5);

, , null ( HasValue DateTime?). , starttime endtime ( , , duration):

string duration = (starttime.HasValue && endtime.HasValue) 
    ? (endtime - starttime).ToString() 
    : "0";
+1

starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) ? "" : rdr.GetString(4);
endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) ? "" : rdr.GetString(5);

starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) ? new DateTime() : rdr.GetDateTime(4);
endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) ? new DateTime() : rdr.GetDateTime(5);
+1

DateTime.Parse, DateTime.

When using Nullable Types , for example DateTime?, you should check if a value exists using a property HasValue, to get the value use a property Value.

string htmllink = "";
htmllink = (dbNullCheck.isColumnNull(rdr, "html_link")) ? "" : rdr.GetString(3);
DateTime? starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) ? default(DateTime?) : DateTime.Parse(rdr.GetString(4));
DateTime? endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) ? (DateTime?)null : DateTime.Parse(rdr.GetString(5));

results.htmllist.Add(new gethtmllist() { resulthtmllink = htmllink, 
duration = starttime.HasValue && endtime.HasValue ? (starttime.Value - endtime.Value).ToString() : "there is no duration" });

To use nulltrenary with null types in an expression, remember to use it for the expected type with (DateTime?)nullor default(DateTime?). See why this is necessary .

+1
source

Source: https://habr.com/ru/post/1016868/


All Articles