Static vs Application in Global.aspx.cs

R

rwoo_98

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
 
M

Marina Levit [MVP]

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.
 
C

Cowboy \(Gregory A. Beamer\)

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!
*************************************************
 
R

rwoo_98

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?




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!
*************************************************
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top