?
Solved

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

Posted on 2011-02-26
7
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

 
LVL 4

Accepted Solution

by:
fromer earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When I was a beginner in Windows Mobile programming, even for small test I used to create a single MFC dialog-based application with one button, and put my test code in the OnLButtonDown method. For Pocket PC it worked fine, on some Windows CE d…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

770 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