Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Singleton

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sathyaish - 16 Jul 2006 14:33 GMT
OK, shame on me, I can't even do a singleton properly. I have
implemented this a thousand dozen times, but I guess I've now been
awake too long.

Here's the relevant snippet. When I compile, it rightly points out that
the member _plain, in the method GetPlain(int, int) requires an object
reference. But I cannot do a this._plain in that method too because it
is a static method.

How on earth do I get out of this circle. I bet this is a trivial thing
and I have done it countless times but just now my brain is revolting.

public class Plain: IRectangularGrid, IDisposable /*It is difficult to
spell Plateau every time, so I am naming it Plain*/
    {
        private Plain _plain = null;    /*Singleton*/

        public static Plain GetPlain(POINT upperRight)
        {
            return GetPlain(upperRight.x, upperRight.y);
        }

        public static Plain GetPlain(int upperRightX, int upperRightY)
        {
            if (_plain == null)
                _plain = new Plain(upperRightX, upperRightY);

            return _plain;
        }

        private Plain(int upperRightX, int upperRightY)
        {
                                      //some stuff
        }

My head is swirling round. Please tell me what I am missing.
Joanna Carter [TeamB] - 16 Jul 2006 14:41 GMT
| My head is swirling round. Please tell me what I am missing.

I can do no better than lead you to Jon Skeet's description which accounts
for thread safety as well.

http://www.yoda.arachsys.com/csharp/singleton.html

Joanna

Signature

Joanna Carter [TeamB]
Consultant Software Engineer

allfyre - 16 Jul 2006 14:42 GMT
I think you want to declare the private variable _plain as static as
well.  I think this should resolve your problem, but I've been awake
for a long time too.

> OK, shame on me, I can't even do a singleton properly. I have
> implemented this a thousand dozen times, but I guess I've now been
[quoted text clipped - 32 lines]
>
> My head is swirling round. Please tell me what I am missing.
Tom Spink - 16 Jul 2006 14:45 GMT
> OK, shame on me, I can't even do a singleton properly. I have
> implemented this a thousand dozen times, but I guess I've now been
[quoted text clipped - 32 lines]
>
> My head is swirling round. Please tell me what I am missing.

Hi Sathyaish,

> private Plain _plain = null;  /*Singleton*/

Define _plain to be static:

///
private static Plain _plain = null;
///

Signature

Hope this helps,
Tom Spink

Google first, ask later.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.