-4

Start date is used in multiple places and it defaults to some value.

Can I declare this as a or constant or static property.

DateTime dtStart = DateTime.Now.AddYears(-2).AddMonths(-1).ToShortDateString();

Edit

string dtStart = DateTime.Now.AddYears(-2).AddMonths(-1).ToShortDateString();

I missed to mention that I want to know its behaviour in multi-threaded environment and changed the datatype to string.

Sunny
  • 4,765
  • 5
  • 37
  • 72
  • Why don't you simply try doing just what you describe and see what happens? – O. R. Mapper Apr 09 '14 at 20:00
  • 1
    constant and static property are two different things. You can't change a constant but you can change a static property. What exactly are you trying to ask ? By the way `ToShortDateString()` would return a string, not a `DateTime` object – Habib Apr 09 '14 at 20:00
  • 1
    Not yet familiar on how to properly ask a question ? http://stackoverflow.com/help – aybe Apr 09 '14 at 20:00
  • It actually works for me. But my doubt is how it works in multi-threaded envionment. – Sunny Apr 09 '14 at 20:02
  • 2
    @Sunny: It would be helpful if you could mention aspects so central to your question as *multi-threading* seems to be here somewhere *in your question*. – O. R. Mapper Apr 09 '14 at 20:06
  • If you're asking about static variable initialization in multi-threaded programs, see here: http://stackoverflow.com/questions/2971211/thread-safe-initialization-of-static-variables – Jon B Apr 09 '14 at 20:11

2 Answers2

0

It can't be constant since the value changes. You can easily make it a static property in your class:

public static string StartDate
{
    get
    {
        return DateTime.Now.AddYears(-2).AddMonths(-1).ToShortDateString();
    }
}

Note: To make it easier to test your code, you should pass the methods the start date instead of having them all call this property.

Also, it seems odd that you want this to return a string...

As for multithreading, each thread will call into this property itself. It will be fine since the property does not modify any state.

David
  • 34,223
  • 3
  • 62
  • 80
0

Try use one class static if exist in your project

 public static class Uitl {

    public static DateTime DateStart = default(DateTime);

    public Uitl (){
      if(DateStart == default(DateTime))
         DateStart = DateTime.Now.AddYears(-2).AddMonths(-1);

    }

 }

OR use

Session["DateStart"] = DateTime.Now.AddYears(-2).AddMonths(-1).ToShortDateString();
Daniel Melo
  • 548
  • 5
  • 12