Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:48:45+00:00 2026-06-05T07:48:45+00:00

I have the next static class: public static class GlobalVar { public static string

  • 0

I have the next static class:

public static class GlobalVar
{
    public static string DatabaseName = "ProjectDatabase.mdf";

    public static AdminClass Admin;

    public static string TruePath = AppDomain.CurrentDomain.BaseDirectory;

    public static string TimeStampPattern = "dd/MM/yyyy HH:mm";

    static GlobalVar()
    {
        TruePath = TruePath.Remove(TruePath.Length - 1);
        Admin = new AdminClass("Admin", "Admin");
        GlobalStatus = new Dictionary<string, string>();
        string Query = "SELECT * FROM global_status";
        DataTable Types = MyAdoHelper.ExecuteDataTable(GlobalVar.DatabaseName, Query);
        foreach (DataRow Status in Types.Rows)
        {
            GlobalStatus.Add(Status["title"].ToString(), Status["info"].ToString());
        }
    }

    public static Dictionary<string, string> GlobalStatus; 

    public static string BasePath = HttpContext.Current.Request.ApplicationPath;
}

The GlobalStatus is a dictionary receives the custom errors the site may return. When I launch the project (Microsoft Visual Web developer 2008), it gives an error:

Object reference not set to an instance of an object.

when trying to get a value from GlobalStatus (eg. GlobalVar.GlobalStatus[“Page_NoAccess”]).

Only after a minute, when I launch the project again, it runs fine.

How can I fix this, like force it the browser to wait until it fills GlobalStatus. I need this class to initialize only once, since it contains global variables which I don’t want to be recalled every browser request.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:48:47+00:00Added an answer on June 5, 2026 at 7:48 am

    First of all, I want to suggest you global vars concept in an object-oriented code is a bad idea.

    In order to solve your problem, you need to use built-in, out-of-the-box ASP.NET approaches: global application class.

    Use Application_Start event handler and invoke initialization processes during this event .

    Anything opposed to above statement is a bad idea.

    Now… how to do it better

    1. For everything that you find that are settings use AppSettings or settings files.
    2. Using static constructors for application initialization is a bad idea. Create a static class with static methods like Initialize(), Start()…. Call them in Application_Start event of global application class.
    3. Static classes with static fields and data in a multi-threaded environment like ASP.NET are a big fail. More than a thread can initialize your application at once. How your code manages this situation?.
    4. Forget global variables concept in an object-oriented piece of software. Use settings (like I said in 1st point) or create constants or fields (static or instance ones) as part of the class that’s going to consume it.

    UPDATE & NOTE
    I suggest this MSDN article about static constructors in order to understand why these are a bad idea in ASP.NET initialization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the next code: class Foo { public Foo (String param) { ...
I have the next piece of code: internal static string GetNetBiosDomainFromMember(string memberName) { int
I have the next code: public static T GetSerializedCopy<T> (T src) { //some code
I have the following java code: public class CheckInnerStatic { private static class Test
I have this register that registers all the objects I need: public static class
I have an extension method with the following signature (in BuildServerExtensions class):: public static
We have this class here: public static class Helper { private static readonly Random
I have next dependency property: public static DependencyProperty RequestObjectProperty = DependencyProperty.Register(RequestObject, typeof(RegistrationCardSearch), typeof(RegCardSearchForm),new UIPropertyMetadata(Changed));
I have next tables Order , Transaction , Payment . Class Order has some
I have next code require 'rack/rpc' class Server < Rack::RPC::Server def hello_world Hello, world!

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.