Click here to Skip to main content
15,896,339 members
Home / Discussions / C#
   

C#

 
GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 2:25
George_George26-Nov-08 2:25 
GeneralRe: implement IDisposable issue Pin
leppie26-Nov-08 2:41
leppie26-Nov-08 2:41 
GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 3:16
George_George26-Nov-08 3:16 
GeneralRe: implement IDisposable issue Pin
leppie26-Nov-08 3:23
leppie26-Nov-08 3:23 
GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 3:31
George_George26-Nov-08 3:31 
AnswerRe: implement IDisposable issue Pin
Scott Dorman26-Nov-08 16:18
professionalScott Dorman26-Nov-08 16:18 
GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 20:58
George_George26-Nov-08 20:58 
GeneralRe: implement IDisposable issue Pin
Scott Dorman27-Nov-08 2:49
professionalScott Dorman27-Nov-08 2:49 
1. I am talking about writing a finalizer. In your class this would be a method named ~Logger(). There is generally no need to write a finalizer, however you should always implement IDisposable if your object maintains unmanaged resources, derives from something that implements IDisposable, or otherwise maintains disposable objects.

2. You are confused with the difference between a static class and a singleton. A singleton is a design pattern that ensures there is ever only one instance of the class. Singletons are not static classes. (Your code also does not implement a static class either...simply a "normal" class with static members. There is a difference.)

3. Again, you are confusing a static class and a singleton. In this scenario, you need to forget about using static anything.

Here is a more complete example of using a singleton:
public sealed class Logger : IDisposable
{
   // This is the actual instance of the logger maintained by the singleton.
   private static Logger instance;
   private StreamWriter logStream;   
   private bool disposed; // No need to initialize here as the default is false.
   private static readonly object syncLock = new object();
    
   private Logger()
   {
   }
    
   public void Dispose()
   {
      this.Uninitialize();
   }
 
   protected virtual void Dispose(bool disposing)
   {
      if (!disposed)
      {
         if (disposing)
         {
            if (logStream != null)
            {
               logStream.Dispose();
               logStream = null; // Not strictly necessary, but won't hurt either.
            }            
         }
      }
      disposed = true;
   }
 
   public void Uninitalize()
   {
      Dispose(true);
      GC.SuppressFinalize(this);
   }
    
   public static Logger Instance
   {
      get
      {
         lock(syncLock)
         {
            if (instance == null)
            {
               instance = new Logger();
            }
         }
         return instance;
      }
   }
}
This shows how you would use the singleton object. The actual class is sealed with a private constructor, so the only way to get it to do anything meaningful is to access it through the static Instance property. This will return a new instance of the Logger class if one hasn't already been created; if one has been created it will return that instance
Logger.Instance.Uninitalize();


Scott Dorman
Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA

[Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

QuestionHow edit my URL on button click Pin
Exelioindia26-Nov-08 1:48
Exelioindia26-Nov-08 1:48 
AnswerRe: How edit my URL on button click Pin
Simon P Stevens26-Nov-08 1:56
Simon P Stevens26-Nov-08 1:56 
AnswerRe: How edit my URL on button click Pin
Dominic Goulet1-Dec-08 1:09
Dominic Goulet1-Dec-08 1:09 
QuestionExhange Server 2007 Webservice Pin
boiDev26-Nov-08 1:33
boiDev26-Nov-08 1:33 
Questioncopying files fast from 1 drive to other Pin
sumit703426-Nov-08 0:59
sumit703426-Nov-08 0:59 
AnswerRe: copying files fast from 1 drive to other Pin
Guffa26-Nov-08 1:14
Guffa26-Nov-08 1:14 
GeneralRe: copying files fast from 1 drive to other Pin
sumit703426-Nov-08 1:21
sumit703426-Nov-08 1:21 
AnswerRe: copying files fast from 1 drive to other Pin
Simon P Stevens26-Nov-08 1:16
Simon P Stevens26-Nov-08 1:16 
AnswerRe: copying files fast from 1 drive to other Pin
Shyam Bharath26-Nov-08 1:33
Shyam Bharath26-Nov-08 1:33 
Questionshowing a linklabel in a messagebox? Pin
lane0p226-Nov-08 0:53
lane0p226-Nov-08 0:53 
AnswerRe: showing a linklabel in a messagebox? Pin
Pedram Behroozi26-Nov-08 1:08
Pedram Behroozi26-Nov-08 1:08 
QuestionCode to find the csc.exe Pin
tonyjsebastian126-Nov-08 0:53
tonyjsebastian126-Nov-08 0:53 
AnswerRe: Code to find the csc.exe Pin
Shyam Bharath26-Nov-08 1:02
Shyam Bharath26-Nov-08 1:02 
AnswerRe: Code to find the csc.exe Pin
Pedram Behroozi26-Nov-08 1:02
Pedram Behroozi26-Nov-08 1:02 
AnswerRe: Code to find the csc.exe Pin
Mirko198026-Nov-08 2:15
Mirko198026-Nov-08 2:15 
GeneralRe: Code to find the csc.exe ======> U r correct Mikro1980 Pin
tonyjsebastian126-Nov-08 17:54
tonyjsebastian126-Nov-08 17:54 
QuestionHow to find the path of the file csc.exe Pin
tonyjsebastian126-Nov-08 0:34
tonyjsebastian126-Nov-08 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.