Solved

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

Posted on 2011-02-26
7
270 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Let me explain this picture a little bit.  First, in case you haven't already guessed, you are looking at my 2 phones, an Android Samsung Galaxy S5 on the left and an iPhone 5 on the right.  They are on their respective cradles on my desk.  But, you…
I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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