-
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-----------------------------------------
-
Have you tried Method A? I don't think static variables work in ASP.NET, because HTTP is stateless, so a new object instance gets created on each request.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
Similar Threads
-
Replies: 4
Last Post: 08-02-2007, 10:08 AM
-
By hkwan in forum ASP.NET
Replies: 5
Last Post: 08-29-2001, 10:17 PM
-
By hkwan in forum ASP.NET
Replies: 0
Last Post: 08-28-2001, 03:18 AM
-
Replies: 0
Last Post: 08-28-2001, 01:19 AM
-
Replies: 1
Last Post: 07-02-2001, 05:07 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks