Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-26
7
Medium Priority
?
302 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

609 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