locked
how can one restore a cached object from within the objects constructor? RRS feed

  • Question

  • User446859115 posted

    Is there a way in an asp.net application to return/restore an object from that object's constructor?

    For example I have an object called "logging" that I can instantiate in my code with it's "new" constructor like thus:

    logging log = new logging();

    and somewhere in my code I've saved this object as a session object

    httpContext.Current.Session["log"] = log;

    Now what I'd like to be able to do is inside the logging constructor, I check for the "saved" session object and if it's there, instead of creating a "new" logging object, I return the one saved in the session object collection (assume session mode is Inproc).

    In my logging constructor I tried this but it doesn't fly:

    public class logging
    {
        private string _mainLogLocation = "";
        private string _subLogLocation = "";
        private level _ReportLevel = level.critical;
    
        public enum level { verbose, inform, warning, error, critical };
        public string[] levels = { "verbose", "informational", "warning", "error", "critical" };
    
        public logging()
        {
            if (HttpContext.Current.Session["log"] != null)
            {
                this = (logging)HttpContext.Current.Session["log"];
                 return;
            }
    //    code to create and setup logging properties
    
        }
     // method and property definitions
    
    } // end of logging class


    The complier does not allow me to make assignemnts to the "this" reference. Any ideas on how to get around this limitation or other ways to accomplish this type of function for this class?

    Wednesday, November 7, 2012 5:27 PM

Answers

  • User840877147 posted
    public class logging
    {
    	private static logging _logInstance;
    
    	public static logging GetInstance()
    	{
    		if (HttpContext.Current.Session["log"] != null)
    			_logInstance = (logging)HttpContext.Current.Session["log"];
    		else
    			_logInstance = new logging();
    
    		return _logInstance;
    	}
    }
    
    //Usage in code
    var log = logging.GetInstance();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 18, 2012 7:09 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 18, 2012 8:19 AM

All replies

  • User840877147 posted
    public class logging
    {
    	private static logging _logInstance;
    
    	public static logging GetInstance()
    	{
    		if (HttpContext.Current.Session["log"] != null)
    			_logInstance = (logging)HttpContext.Current.Session["log"];
    		else
    			_logInstance = new logging();
    
    		return _logInstance;
    	}
    }
    
    //Usage in code
    var log = logging.GetInstance();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 18, 2012 7:09 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 18, 2012 8:19 AM