Question

I read on this question and answer, where you must create a new DateTime object if you need to modify (or shift) the time of the object, because DateTime is an immutable object type.

Why DateTime.AddHours doesn't seem to work?

That's fine if the variable is being declared and a value is being set in the same lexical scope. Except I'm declaring and setting the value in different lexical scope. Normally I'd do "DateTime from_instant = null;" so it at least has a value, so I can use it after it's set. However, with DateTime, once you set the value, it's immutable. So how can I adjust the value of my object by doing "from_instant.AddHours(-10);" for example, outside the lexical scope it was declared in?

In my case below, whenever I reset "from_instant", it doesn't change. I'd like to be able to change it. How do you typically reset the value since it's immutable?

    DateTime from_instant = DateTime.Now;

    bool set_scan_start_instant_to_last_scan_instant = Convert.ToBoolean(GetConfig("set_scan_start_instant_to_last_scan_instant"));

    if (set_scan_start_instant_to_last_scan_instant)
    {
        from_instant = GetScanTimeFromFile(@".\last_scan_instant.txt");
        from_instant = DateTime.Now;
    }
    else
    {
        if (!string.IsNullOrEmpty(scan_interval_minutes))
        {
            from_instant = DateTime.Now.AddMinutes(Convert.ToInt32(scan_interval_minutes));
        }
        else if (!string.IsNullOrEmpty(scan_interval_hours))
        {
            from_instant = DateTime.Now.AddHours(Convert.ToInt32(scan_interval_hours));
        }
        else if (!string.IsNullOrEmpty(scan_interval_days))
        {
            from_instant = DateTime.Now.AddDays(Convert.ToInt32(scan_interval_days));
        }
    }

    DateTime to_instant = DateTime.Now;
    WriteScanTimeToFile(@".\last_scan_instant.txt", to_instant.ToString());

    Console.WriteLine("from_instant: " + from_instant.ToString());  
    Console.WriteLine("to_instant: " + to_instant.ToString());        
Was it helpful?

Solution

The problem is you keep calling methods on DateTime and ignoring the return values. Don't do that.

Change this:

from_instant.AddHours(Convert.ToInt32(scan_interval_hours));

to

from_instance = from_instant.AddHours(Convert.ToInt32(scan_interval_hours));

Or:

fromInstance += TimeSpan.FromHours(Convert.ToInt32(scan_interval_hours));

(etc). It's not clear why you appear to think you can't do this. It's also not clear what you expect this code to do:

from_instant = GetScanTimeFromFile(@".\last_scan_instant.txt");
from_instant = DateTime.Now;

Why would you assign a value to from_instant and then immediately assign a different value?

(Side-note: underscores are pretty unidiomatic in C# code.)

In my case below, whenever I reset "from_instant", it doesn't change.

Sounds like you're probably not diagnosing things properly. Changing the value of the variable really will change it...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top