Overriding DateTime.MinValue problem

We are trying to override DateTime.MinValue

in our application, but while doing so we noticed that our web services are timeouts, the following code example. Not sure what is wrong / what we are missing.

 public MainWindow()
 {
     //Introducing this.. Causes timeout of the webservice call...
     typeof(DateTime).GetField("MinValue").SetValue(typeof(DateTime),new DateTime(1900, 1, 1));
     var yesitworks= DateTime.MinValue;
     InitializeComponent();
     ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
     //Below call will timeout...
     var value =client.GetData(10);
}

      

PS: This might not be the best solution for what we are trying to solve, and now its more curious as to why it is not working? How is this related.

+3


source to share


3 answers


DateTime.MinValue

- static field readonly. This means that the authors of the library do not expect it to change and can write code that depends on it with the expected value.

Hence, you should not change the value DateTime.MinValue

.

For example, a library can use it as the default value for a variable:

private mostRecentDate= DateTime.MinValue;
foreach (var date in myDates)
{
    if (date > mostRecentDate)
    {
        mostRecentDate= date;
    }
}

// Do something with the most recent date in myDates...

      



In this example, if myDates

contains dates before your new value for DateTime.MinValue

, then this code set mostRecentDate

to DateTime.MinValue

, and not the last date to myDates

.

While this rather contrived example might not be good programming practice ( for example , you can use Nullable instead), this is valid code whose behavior would change if you changed the value DateTime.MinValue

.

The point is that the libraries you are using can also depend on the value on DateTime.MinValue

, so changing it can break them. Unfortunately, you still believe that this led to an error. If you're out of luck, you won't see a problem until your software goes live and some corner case has been hit.

+3


source


I recently had a similar problem.
You didn't say why you want to override DateTime.MinValue

, but I think the reason is similar to mine:

I have a server written in .NET that has .NET clients and (via COM-Interop) MS Access clients.

Clients pass values DateTime

, and the server needs to check if they passed the "real" value or DateTime.MinValue

.

My problem:

  • .NET DateTime.MinValue

    - January 1 of year 1
  • The smallest possible value for a VBA type Date

    is January 1 of the year 100

⇒ Checking for DateTime.MinValue

did not work when the data came from MS Access, because variables Date

in Access cannot contain a date like .NET DateTime.MinValue

.

At this point I tried to override DateTime.MinValue

too, and found out that it doesn't work.



My solution was to write an extension method for DateTime

:

public static class DateTimeExtensions
{
    public static bool MinValue(this DateTime input)
    {
        // check the min values of .NET *and* VBA
        if (input == DateTime.MinValue || input == new DateTime(100, 1, 1))
        {
            return true;
        }

        return false;
    }
}

      

For the code in your question, it should look like this:

public static class DateTimeExtensions
{
    public static bool MinValue(this DateTime input)
    {
        if (input == new DateTime(1900, 1, 1))
        {
            return true;
        }

        return false;
    }
}

      

Using:

DateTime theDate = DateTime.Now;

// vanilla .NET
bool isMin1 = (theDate == DateTime.MinValue);

// with the extension method
bool isMin2 = theDate.MinValue();

      

+1


source


I don't think you can change DateTime

MinValue

since it is read only, but if you can NOT NOT

DateTime:

public struct DateTime : IComparable, IFormattable, IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime>
{
    public static readonly DateTime MaxValue
    public static readonly DateTime MinValue
    ....

      

-1


source







All Articles