Solved

C# how to access one class/object across entire application.

Posted on 2011-02-26
7
248 Views
Last Modified: 2014-11-12
I have an C# application written for Windows Phone 7 and I have multiple classes. I have created a singleton class for logging and I would like to Instantiation the logger class one time and use that instance throught out the application even as other classes come and go. I am not sure where in the application to create this object that I would be sure that it will stay 'alive' and be acceptible by all other classes during runtime.

Examples help the best.

Thank You
0
Comment
Question by:atomicgs12
7 Comments
 
LVL 2

Expert Comment

by:Raymond-Holmboe
ID: 34991241
That is the purpose of a singleton, it creates just one instance that is alive "forever". Normally you dispose your singletons at app exit, if you need to.
0
 
LVL 4

Expert Comment

by:fromer
ID: 34991308
   public sealed class Logger
    {
        public static readonly Logger Instance = new Logger();
        private List<string> Logs;
        private Logger()
        {
            Logs = new List<string>();
        }
        public void AddLog(string log)
        {
            Logs.Add(log);
        }
    }
    public class Class1
    {
        public Class1()
        {
            Logger.Instance.AddLog("Class1 constructor");
        }
    }
    public class Class2
    {
        public Class2()
        {
            Logger.Instance.AddLog("Class2 constructor");
        }
    }

0
 

Author Comment

by:atomicgs12
ID: 34992965
fromer:

Is there some where in the application that one could declare Logger.Instance as a global so the code doesn't have Logger.Instance.AddLog all over the place? Like some where on startup declare a global, g_Logger = Logger.Instance;, then throught out the code just write g_Logger.AddLog("test");. Better yet I'd like to be able just to write AddLog("test") where needed.

Thanks
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Accepted Solution

by:
fromer earned 500 total points
ID: 34995079
There is no such thing in c#, since every variable must be declared as a class member, or in a method..
my suggesstion is:

public static class g_Logger
{
  public static void AddLog(string log)
  {
    Logger.Instance.AddLog(log);
  }
}

You can't do more than this in c#...
0
 

Author Comment

by:atomicgs12
ID: 35004025
So then it would be g_Logger.AddLog("test");
0
 
LVL 16

Expert Comment

by:hjgode
ID: 39626909
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I think the most popular question in our Windows Mobile Programming zone is about the backlight - we all want to keep it on when our application is running. Few years ago the function SystemIdleTimerReset() did this job. On our side we had to detec…
If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now