Take the 2-minute tour ×

I have a date value that I need access to throughout my C# website. I have stored it in the Global.asax file like this:

<script runat="server">

void Application_Start(object sender, EventArgs e) 
    // Code that runs on application startup
  Application["AppNoApptDate"] = "12/1/2050 10:00:00 AM";


To access it as a date I do this:

string tmp = (String)Application["AppNoApptDate"];
DateTime noApptDate = Convert.ToDateTime(tmp);

and use "noApptDate" as I need to.

"noApptDate" will never change. But is how I am accessing the application variable and converting it to a DateTime the most efficient code? Can't this be done in one line instead of two?

share|improve this question
Read the data from a database, or web config file –  asawyer Feb 4 at 17:09
Why do that versus Global.asax or creating a GlobalVars class? –  user995727 Feb 4 at 17:13
Why publish a site with a non-constant date hard coded when it can be manage with a config key so easily? –  asawyer Feb 4 at 17:21
I'm not sure. It seems there are several ways to do this. XML file, db, web.config, custom class, global.asax. I don't know what the best route to go is. "non-constant date", in this case the date will never change. –  user995727 Feb 4 at 17:24
Then declare it public const in a public class and be done with it. –  asawyer Feb 4 at 18:07
add comment

1 Answer

up vote 0 down vote accepted

There's no need to store it as string. You can store it as DateTime directly, and you'll avoid the need to do the parsing. As long as you're not on a cluster, this should work relatively OK (although do note that if you set the date to something like DateTime.Now during application start, the value might change as the application pool recycles).

However, if you actually want to set the value at compile time, it would be easier to just have a static field / property in a class somewhere with the value.

share|improve this answer
Is that a better design to make a "Globals" class and set static field/property rather than using the Global.asax file? –  user995727 Feb 4 at 17:04
@user995727 You can actually put it inside Global.asax just fine - it's a class like any other (it inherits HttpApplication). You can even access the HttpApplication instance using HttpContext.Current.ApplicationInstance and cast it to any type you want. The reason that Application["..."] is preferred is because it takes scenarios such as IIS in a web farm into account. So yeah, feel free to put it anywhere you feel it belongs - for a static value, it's not really a problem :) –  Luaan Feb 4 at 17:27
Thank you for the explanation. Can you show me how to condense my two lines from above where I use the application variable? I declare it as a string then as a datetime, I'm sure there's a better way to do that. –  user995727 Feb 4 at 17:36
@user995727 Example: public static readonly DateTime AppNoApptDate = new DateTime(2050, 12, 1); Or if you want to use the same syntax you use, Application["AppNoApptDate"] = new DateTime(2050, 12, 1); and then you can read it like this - (DateTime)Application["AppNoApptDate"]. –  Luaan Feb 4 at 17:39
Awesome, thank you! –  user995727 Feb 4 at 18:35
show 1 more comment

Your Answer


