Link to home
Start Free TrialLog in
Avatar of SamCash
SamCashFlag for United States of America

asked on

instantiate object in class area

How to instantiate in class declaration area?  Or test if already instantiated?

I did this so it would be available (scoped) to all the code in all the pages.

namespace WebApplication1
{
    public partial class ManageRosters : System.Web.UI.Page
    {
        SessionLog StatusLog = new SessionLog();

    protected void Page_Load(object sender, EventArgs e)
        {

Open in new window


The program dies here without notice.  I think it is wrong to re-instantiate, but things die here the second time??

Regards
Sam
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Hi,

This can be accessed in the current page anywhere as you have initialized globally.

SessionLog StatusLog = new SessionLog();

In other pages you cannot access the SessionLog untill or Unless you do the same in other pages as well.
ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SamCash

ASKER

Lokesh, Easwaran,

First thank you for the quick response.

Lokesh, yes that is why I have put it there.  I have never instantiated an object in that area.  I have not been able to test object == null in that area, so it gets instantiated on every page post, weather or not is needs to.  With vs2012 ("vs") when I "f11" on that line the second time through it appears to abort or post back, as nothing happens, ie, the vs "continue" button becomes disabled and the web page is active, (the event that caused the post is not executed) I can click another button and control will be transferred back to vs, I can break on that line, vs "continue" becomes enabled, press f11, vs "continue" is disabled, and we are back at the browser, without executing the event.  No exceptions, error, just silently failing.  I planned on Instantiating at the top of each page, the objective or result will be an Application Event Log that spans all pages in the Session.

throughout the page I can append to the log with

StatusLog.StatusLogUpdate("Must have a valid UID and LID", DebugFlag); //appends this message to top of status log

Open in new window


namespace WebApplication1.myUtilities
{
    public class SessionLog : System.Web.UI.Page
    {
        public string StatusLog
        {
            get
            {
                return (string)(Session["StatusLog"]);
            }
            set { }
        }
        public void StatusLogUpdate(string AppendMessage, bool DebugFlag)
        {
            string StatusLog, Result;
            int Length = 1000;

            if (DebugFlag)
            {
                StatusLog = (string)(Session["StatusLog"]);
                Result = StatusLog == null || StatusLog == "" ? StatusLog = AppendMessage : StatusLog = AppendMessage + "<br />" + StatusLog;
                Result = (Result.Length < Length) ? Result : Result.Substring(0, Length);
                Session["StatusLog"] = Result;
            }
            return;
        }
    }
}

Open in new window


Easwaran, I am not fluent with singletons, I will study today and weekend,  Will there be an instance per session or page could work, if there is literally only one, then other users, (other session) will write to the same log?  Recommended reading is welcome?

Thanks again,
Sam
Avatar of SamCash

ASKER

Easwaran,

I trust you had a good weekend.  I have read quite a bit on singletons.  I was not able to find anything on point.  Most are implying there is only one per application.  Which would be a problem as all sessions will be using it and could mix log entries however the fact the singleton is writing to each sessions variables it could keep the logs on a per session bases and not mix things up.  I am looking for expert advice as this will be very difficult to test with multiple concurrent sessions.  Will this work for my application?

Kind Regards
Sam