none
c# asp.net static variables

    Question

  • i'm a little confused with c# static variables. i understand what static variables are, however it seems their lifetime goes beyond the lifetime of the page call (in asp.net).

    eg. a static variable in some class

    public static int m_test;

    EXAMPLE SENARIO:
    1. call the page and print the variable (value is 0)
    2. call the page again and set the variable to 100
    3. call the page again and print the variable (the value is now 100)

    I am a little concerned because I am assuming the static variables exist in the scope/lifetime of a single page call. however with the above example, i can no longer assume that the variable's value is 'clean' (because it may have been set with previous calls)

    this is fine because i can explicitly initialise the variable to 0.

    my question is, how does this static variable work with pages executing at the same time? are multiple instances of this variable created? or if i initialise to 0, am i overwriting the value in other threads?
    Sunday, April 09, 2006 4:24 AM

Answers

  • Static variables scope is across the scope of the server, not for the life of a call to a particluar page, as you have discovered. If you set the value once, all subsequent calls made to that instance of the aspnet_wp.exe process will have access to this value.

    What are you trying to do? It seems that you should use instance variables instead.
    Sunday, April 09, 2006 4:53 AM

All replies

  • Static variables scope is across the scope of the server, not for the life of a call to a particluar page, as you have discovered. If you set the value once, all subsequent calls made to that instance of the aspnet_wp.exe process will have access to this value.

    What are you trying to do? It seems that you should use instance variables instead.
    Sunday, April 09, 2006 4:53 AM
  • ye. this has really scrwd with my design.

    i am using static classes/singletons/facades to represents a number of things.

    for example to access the conenction

    DBConnection.Open();
    DBConnection.Execute();
    DBConnection.Close();

    and more..

    LibForm.GetInt()
    LibForm.GetString()
    etc...

    LibQueryString.GetInt()
    LibQueryString.GetString()
    etc...

    the list goes on... :S

    the aim was to save passing these objects through the nested function calls. which seemed logical since these objects represent singletons/facades.

     

     

    Sunday, April 09, 2006 5:00 AM
  • I use a static function to get my DB Connection string, but it returns an instance of a connection each time. It's OK to do this because ADO.NET will manage connection pools for you anyway - so even though you close an instance of a DB connection, it's not really closed, it just sits in the pool inactive until the next ADO.NET client wants to use a connection. Here is the code I use (in a common class) to get my DB connection string:

            private static string _dbConnectionString = null;

            /// <summary>
            /// Reads the database connection string for the WebQuester database from the registry
            /// </summary>
            /// <returns>SQL Connection object (not connected) for the database</returns>
            public static SqlConnection GetDBConnection()
            {   
                SqlConnection    connection = null;

                try
                {
                    // Get connection string if required
                    if(_dbConnectionString == null)
                    {
                        // get connection
                        _dbConnectionString = GetValueFromRegistry(Constants.Registry.Value.DBConnectionString).ToString();

                        /// As this is the first time to get the database connection
                        /// string - verify that it works by attempting to open and close
                        /// the DB connection
                        connection = new SqlConnection(_dbConnectionString);
                        try
                        {
                            connection.Open();
                        }
                        catch(Exception ex)
                        {
                            // Ensure value is re-read from registry next time around
                            _dbConnectionString = null;
                            // Clear all existing connection pools
                            System.Data.SqlClient.SqlConnection.ClearAllPools();
                            // Log exception
                            LogExceptionToEventLog(
                                ex,
                                EventLogEntryType.Error);
                            // Bubble-up error
                            throw;
                        }
                        connection.Close();

                    }

                    /// Create connection if it hasn't already been
                    /// created
                    if(connection == null)
                    {
                        connection = new SqlConnection(_dbConnectionString);
                    }

                    // return connection
                    return connection;
                }
                catch (Exception ex)
                {
                    // Log and rethrow the error
                    LogExceptionToEventLog(ex, EventLogEntryType.Error);
                    throw;
                }
            }

    Sunday, April 09, 2006 5:46 AM