467,923 Members | 1,459 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Static vs Application in Global.aspx.cs

Does anyone know the difference between defining a static variable
inside Global vs using an Application variable?

For example

Using a static int variable.

public class Global : System.Web.HttpApplication
{
public static int MaxValue

protected void Application_Start(Object sender, EventArgs e)
{
MaxValue = 100;
}
}

referring to this in your other classes using Global.MaxValue
vs

public class Global : System.Web.HttpApplication
{
public static int MaxValue

protected void Application_Start(Object sender, EventArgs e)
{
Application["MaxValue"] = 100;
}
}

referring to this in your other classes using
(int)Application["MaxValue"]
Thanks in advance

Aug 16 '06 #1
  • viewed: 1608
Share:
3 Replies
I think the end result is going to be the same.

Using an actual variable, you get type safety, where as in Application you
have to cast everything. If you use a static variable, depending on what
class it is defined, you may need to reference that DLL, whereas with
Application, that is a property in Page and UserControl.

If you know your variables ahead of time, and their names aren't going to be
generated by your application, having static variables (or properties) will
make your code easier to read, easier to use those variables, and less
likely that you will have to deal with type issues.

<rw*****@yahoo.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Does anyone know the difference between defining a static variable
inside Global vs using an Application variable?

For example

Using a static int variable.

public class Global : System.Web.HttpApplication
{
public static int MaxValue

protected void Application_Start(Object sender, EventArgs e)
{
MaxValue = 100;
}
}

referring to this in your other classes using Global.MaxValue
vs

public class Global : System.Web.HttpApplication
{
public static int MaxValue

protected void Application_Start(Object sender, EventArgs e)
{
Application["MaxValue"] = 100;
}
}

referring to this in your other classes using
(int)Application["MaxValue"]
Thanks in advance

Aug 16 '06 #2
One is globally attached to the Application object as an object and the
other is typed properly. That is about it.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside the box!
*************************************************
<rw*****@yahoo.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Does anyone know the difference between defining a static variable
inside Global vs using an Application variable?

For example

Using a static int variable.

public class Global : System.Web.HttpApplication
{
public static int MaxValue

protected void Application_Start(Object sender, EventArgs e)
{
MaxValue = 100;
}
}

referring to this in your other classes using Global.MaxValue
vs

public class Global : System.Web.HttpApplication
{
public static int MaxValue

protected void Application_Start(Object sender, EventArgs e)
{
Application["MaxValue"] = 100;
}
}

referring to this in your other classes using
(int)Application["MaxValue"]
Thanks in advance

Aug 16 '06 #3
Thanks all. This is basically what I thought. Cowboy glad to see your
active. I don't know if you recall but I enjoyed discussing the
differences between C# and VB.Net on a previous post that I started a
couple of years ago.

This is a side note, but are you seeing .Net Web Services returning
typed datasets out in the real world. I've done some research and it
seems that the tool sets out there on the java world are not fully
supporting typed datasets (using the xsd to convert the dataset to the
equivalent java class). I haven't done Java since 1997 and haven't
setup a Linux/java environment to consume a .Net web service returning
a dataset.

I am assuming that Microsoft wants typed datasets to be a standard.
However the java world is somewhat reluctant to fully support it. If
they do, and a large number websites are done in .Net returning typed
datasets, the will be forced to support Microsoft's tweaking of
datasets in the future. And because I want it easy for .Net and java
developers to consume my web services, I am returning, simple class
(structures) arrays.

Wondering what your thoughts are?

Cowboy (Gregory A. Beamer) wrote:
One is globally attached to the Application object as an object and the
other is typed properly. That is about it.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside the box!
*************************************************
<rw*****@yahoo.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Does anyone know the difference between defining a static variable
inside Global vs using an Application variable?

For example

Using a static int variable.

public class Global : System.Web.HttpApplication
{
public static int MaxValue

protected void Application_Start(Object sender, EventArgs e)
{
MaxValue = 100;
}
}

referring to this in your other classes using Global.MaxValue
vs

public class Global : System.Web.HttpApplication
{
public static int MaxValue

protected void Application_Start(Object sender, EventArgs e)
{
Application["MaxValue"] = 100;
}
}

referring to this in your other classes using
(int)Application["MaxValue"]
Thanks in advance
Aug 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.