DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2005
    Posts
    37

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

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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

  1. Application Variable
    By geo039 in forum .NET
    Replies: 4
    Last Post: 08-02-2007, 10:08 AM
  2. Replies: 5
    Last Post: 08-29-2001, 10:17 PM
  3. Replies: 0
    Last Post: 08-28-2001, 03:18 AM
  4. Replies: 0
    Last Post: 08-28-2001, 01:19 AM
  5. Replies: 1
    Last Post: 07-02-2001, 05:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links