Solved

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

Posted on 2011-02-26
7
291 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
Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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