Which is better between static variable and Application variable?
In my website http://www.hothelpdesk.com , I hope to set a global variable to store domain. so I can access the value in my code anywhere.
I think both Method A and Method B can do that!
In Method A, I use static variable HotHelpDesk.BLL.PublicPar.MyDomain, so I can access the value of HotHelpDesk.BLL.PublicPar.MyDomain in my code anywhere.
In Method B, I use Application["MyDomain"], so I can also access the value of Application["MyDomain"] in my code anywhere.
Could you tell me what different between static variable and Application variable?
Could you tell me which one is better?
Many thanks!
//--------------------------Global.asax------------------------------------------
void Application_Start(object sender, EventArgs e)
{
// Method A
HotHelpDesk.BLL.PublicPar.MyDomain = HotHelpDesk.BLL.Par.GetDomian();
// Method B
Application["MyDomain"] = HotHelpDesk.BLL.PublicClass.GetDomian();
}
//--------------------------Global.asax------------------------------------------
//--------------------------PublicPar.cs-----------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
namespace HotHelpDesk.BLL
{
public class PublicPar
{
public PublicPar()
{
}
public static string MyDomain;
public static string GetDomian()
{
string Full = HttpContext.Current.Request.Url.ToString();
string ToBoot = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
ToBoot = ToBoot.Substring(2);
Full = Full.Substring(0, Full.LastIndexOf(ToBoot));
return Full;
}
}
}
//--------------------------PublicPar.cs-----------------------------------------
abhishek trivediPosted Sep 18, 2007, 1:06 AM
it depends upon how you are using that variables in your application.
means if they are used in your entire aplication it is better to use as application variables since youdont have todeclare them every time.
but if they are going to used for only one form so no meaning declare as application level.
if u r thinking in any other way reply me.