Take the 2-minute tour ×

I am trying to use an extension method to initialize a datetimepicker control to being on the previous work day from today. However this is causing my form design window to show an error as it cannot find the extension method.

You can quickly replicate this error by creting a new windows form, adding a datetimepicker. Then create the following new class to extend DateTime to add a NextDay() method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExtensionMethodDesignWindowTest
    public static class DateTimeExtensions
        public static DateTime NextDay(this DateTime date)
            return date.AddDays(1);

And finally in Form1.designer.cs add the line

this.dateTimePicker1.Value = DateTime.Today.NextDay();

under the datetimepicker1 section. (edit: and also using.system; at the top)

When you navigate back to the form design window you should see the error. Note that this code builds fine and runs correctly, with the datetimepicker showing tomorrow's date when the form loads. Also if you replace the added line in Form1.designer.cs with

this.dateTimePicker1.Value = DateTime.Today.AddDays(1);

Then you do not get the error in the design window. This makes me assume that the error is because the windows form design window can't see the ExtensionMethodDesignWindowTest namespace.

How can I make the form design window be able to see my extension methods?

Thanks Dan

btw though I doubt it matters here is some IDE version info for what I'm using: Microsoft Visual Studio 2010 Version 10.0.30319.1 RTMRel Microsoft .NET Framework Version 4.0.30319 RTMRel

share|improve this question
Generally it's bad practice to put your own code in the designer - VS may regenerate it and you'd lose your changes. Can't you invoke it from Form_Load or something similar? –  kaj Mar 16 '12 at 7:55
Thanks, that's a good point. It works perfectly from Form_Load. If you reply as an answer I'll mark it as correct. –  Dan Mar 16 '12 at 8:00
Moved to an answer. Thanks –  kaj Mar 16 '12 at 8:11
add comment

2 Answers

up vote 1 down vote accepted

Generally it's bad practice to put your own code in the designer - VS may regenerate the designer code and then you'd lose your changes.

You should be able to invoke your extension method from eg Form_Load ie

this.dateTimePicker1.Value = DateTime.Today.NextDay();
share|improve this answer
add comment

You should add the namespace ExtensionMethodDesignWindowTest in your form1, have you done it?

In the designer.cs add:

using ExtensionMethodDesignWindowTest;


Modify your code like this and will work:

this.dateTimePicker1.Value = ExtensionMethodDesignWindowTest.DateTimeExtensions.NextDay(DateTime.Today);
share|improve this answer
I only have one namespace, namely ExtensionMethodDesignWindowTest. So the Form1.cs and designer.cs etc are already in that namespace. I tried adding that line at the top anyway, it did not change anything I'm afraid. –  Dan Mar 16 '12 at 7:46
See my edit, works for me. –  Arnaud F. Mar 16 '12 at 7:56
Thanks for the help Arnaud, that does indeed remove the error however it kind of defeats the point of using an extension method. The suggestion by KAJ makes more sense to me. –  Dan Mar 16 '12 at 7:59
add comment

Your Answer


