David Smithson
David Smithson

Reputation: 464

Set default datetime parameter in function to use current year

I'm coding functions that determine whether a given date is in the previous/same/next year, and these functions might have a different "start of year" date (financial years, chinese new years etc.).

Given that the code (as an example) reads:

public static class ValidationFunctions
{
...
    public static bool IsPreviousYear(DateTime value)
    {
        DateTime defaultStartOfYear = new DateTime(DateTime.Now.Year, 1, 1);
        return IsPreviousYear(value, defaultStartOfYear);
    }
...
    public static bool IsPreviousYear(DateTime value, DateTime startOfYear)
    {
        return value >= startOfYear.AddYears(-1) && value < startOfYear;
    }
...
}

How do I change this function (or what, where and how do I add to the class), to perform the equivalent of:

public static class ValidationFunctions
{
...
    public static bool IsPreviousYear(DateTime value)
    {
        DateTime defaultStartOfYear = new DateTime(DateTime.Now.Year, 1, 1);
        return IsPreviousYear(value, defaultStartOfYear);
    }
...
    public static bool IsPreviousYear(DateTime value, DateTime startOfYear = new DateTime(DateTime.Now.Year, 4, 5))
    {
        return value >= startOfYear.AddYears(-1) && value < startOfYear;
    }
...
}

I understand that the above code throws an ArgumentException and ArgumentOutOfRangeException (mostly due to the DateTime.Now.Year component), hence the question. I've read similar questions that provide the current date, but I just need the year; the similar questions also don't explicitly cover my requirement, or don't seem to at any rate.

Upvotes: 1

Views: 1300

Answers (3)

David Smithson
David Smithson

Reputation: 464

Until this gets closed off as a duplicate question, I'll post the question it duplicates as an answer (I don't like having unanswered questions on my account):

How can I create an optional DateTime parameter?

Upvotes: 0

jods
jods

Reputation: 4591

You have to understand the default values are substitued at compile-time by the C# compiler; not at runtime by the JIT or CLR.

Hence it's impossible to have a dynamic optional parameter (e.g. based on current year).

Upvotes: 0

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

You use a nullable parameter and give it a default value of null:

public static bool IsPreviousYear(DateTime value, DateTime? startOfYear = null)
{
    startOfYear = startOfYear ?? new DateTime(DateTime.Now.Year, 4, 5);
    return value >= startOfYear.Value.AddYears(-1) && value < startOfYear.Value;
}

Default values for parameters have to be constant, and there is no way to specify a constant DateTime value, so the only value left that is possible is to use null with a nullable DateTime.

Even if it was possible to specify a default value for a DateTime parameter, other than null for a nullable DateTime, you would still have to specify a constant value, so you would not be able to handle the "current year" part of it anyway.

Upvotes: 2

Related Questions