Re: Implementation of Singleton
- From: Tony W <someone@xxxxxxxxxx>
- Date: Wed, 01 Jun 2005 17:58:33 +0100
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,
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
.
- Follow-Ups:
- Re: Implementation of Singleton
- From: John B
- Re: Implementation of Singleton
- 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
- Prev by Date: Re: Duplicate Guid
- Next by Date: Re: Sybase connection form Visual.net C#
- Previous by thread: Re: Implementation of Singleton
- Next by thread: Re: Implementation of Singleton
- Index(es):
Relevant Pages
|