Re: Implementation of Singleton
- From: John B <jbngspam@xxxxxxxxx>
- Date: Thu, 02 Jun 2005 09:29:56 +1000
Tony W wrote:
Hi,
Thanks for all the input but it has now gone way over my head. I only have a basic understanding of classes and was hoping that I would be able to learn as I went along.
I have already seen the webpage recommended but this doesn't tell you how to use a Singleton in practice, just how to create it. Maybe I just don't have the experience yet.
I initially wanted to use a global variable to hold the location of the log file. This is simple as you set its value once and then use it wherever you want. As the value will never change there is very little risk of the set value becoming corrupt.
I know that the general concensus is that global variables are 'bad' so I looked on the internet and discovered Singletons were a suggested option. This is why I have arrived at this point. Why are global variables considered 'bad', surely they have a place. Why add loads of extra code that needs executing when it isn't needed?
Your thoughts and advice are really appreciated, please help further.
Many thanks,
Hi Tony, E-mail me to
johnbaro at gmail .. com
and I will email you a zipped working sample of a singleton and its usage if that will help.
Its always easier to "see" a working sample.
You are on the right track BTW with using a singleton for "global" config details (IMO).
:)
Cheers
JB
Tony W
On Wed, 01 Jun 2005 16:40:21 +1000, John B <jbngspam@xxxxxxxxx> wrote:
Jon Skeet [C# MVP] wrote:
John B <jbngspam@xxxxxxxxx> wrote:
public static DebugLog Instance { get {
if(m_Instance == null)
{
m_Instance = new DebugLog();
//Question Jon, can you set a readonly field like this? Havent tried, no time at the mo, will try later.
}
No - but the above isn't thread-safe anyway :) (You'd need to lock on something for the duration of the property call.)
I know it isnt thread safe ;) thanks to some initial reading and thinking re your singleton page.
return m_Instance; } }
If this does not work, or just because you want to, use: private static readonly DebugLog m_Instance = new DebugLog();
That's much better - you could even just make it
public static readonly DebugLog Instance = new DebugLog();
and get rid of the property entirely, if you're convinced you'll never want to do anything else.
Im never convinced that Ill never want to do anything else ;)
Thanks for the input JB
.
- References:
- Re: Implementation of Singleton
- From: John B
- Re: Implementation of Singleton
- From: Jon Skeet [C# MVP]
- Re: Implementation of Singleton
- From: John B
- Re: Implementation of Singleton
- From: Jon Skeet [C# MVP]
- Re: Implementation of Singleton
- From: John B
- Re: Implementation of Singleton
- From: Tony W
- Re: Implementation of Singleton
- Prev by Date: *solved* InvokeMember problem
- Next by Date: how prevent windows title bar became inactive?
- Previous by thread: Re: Implementation of Singleton
- Next by thread: System.StackOverflowException
- Index(es):
Relevant Pages
|