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
SamCashAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
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.
Easwaran ParamasivamCommented:
You have to implement singleton pattern here. Please find below example.
/public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SamCashAuthor Commented:
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
SamCashAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.