Hmmm... I'm not sure I can pinpoint exactly where the problem is in your code. But there is some weirdness there around where and how that _instance variable of yours is getting instantiated. It seems like there's nothing to enforce it only gets created once or doesn't get overwritten multiple times by different SecondClass object.
What I can do is give you some sample code (totally stolen, incidentally from "Advanced ActionScript 3 with Design Patterns" by Joey Lott) that works for me... package { public class MySingletonThing { private static var _instance:MySingletonThing public function MySingletonThing() { // Initialize functions can go here... } public static function getInstance():MySingletonThing { if (MySingletonThing ._instance == null) { MySingletonThing ._instance = new MySingletonThing (); } return MySingletonThing ._instance; } } } ...which you can then call from anywhere using code like... var foo:MySingletonThing = MySingletonThing.getInstance(); ...actually, that's a little too oversimplified. I also include a little SingletonEnforcer class that helps to ensure somebody doesn't incorrectly call "new MySingletonThing()" outside of that class... package { public class MySingletonThing { private static var _instance:MySingletonThing public function MySingletonThing(enforcer:SingletonEnforcer) { // Initialize functions can go here... } public static function getInstance():MySingletonThing { if (MySingletonThing ._instance == null) { MySingletonThing ._instance = new MySingletonThing (enforcer:SingletonEnforcer); } return MySingletonThing ._instance; } } } // Really important! Make sure you include this SingletonEnforcer class in your MySingletonThing.as file class SingletonEnforcer{}; Hope this helps... --T On Fri, Nov 21, 2008 at 9:49 AM, Fabio Pinatti <[EMAIL PROTECTED]> wrote: > Sure thing! > > In my main document class, I have > > class BaseClass { > > public function BaseClass() { > > } > > } > > > I have a second document class, that is loaded inside the first one. > > class SecondClass { > > public static var _instance:SecondClass; > > public function SecondClass() { > _instance = this; > } > > public static function getInstance():SecondClass { > return _instance; > } > > > } > > and my third document class, that is a movie loaded inside that same > document as above. > > class ThirdClass { > > public function ThirdClass() { > trace (SecondClass.getInstance()); > } > > > } > > In Gaia, these 3 classes are respectively Main.as, Nav.as and HomePage.as. > In that sample I wrote, the output is null. If I declare in my base class, > something like private var _it:SecondClass, even I didn't init that var, > and > compile the app again, magically, it outputs rightly my class instance. The > weird thing is, for example, If I try the same getInstance() from > BaseClass, > for example, it returns correctly too. Im wondering why some classes you > can > get instance, and for other, you must declare a var, like my workaround... > > Thats weird, no?? > > Thanks, > > Pinatti > > On Fri, Nov 21, 2008 at 12:39 PM, Hans Wichman < > [EMAIL PROTECTED]> wrote: > > > Hi Fabio, > > > > could you maybe post 2 examples, one that works, and one that doesn't? > > A picture says more than a thousand words:) > > > > greetz > > JC > > > > On Fri, Nov 21, 2008 at 2:19 PM, Fabio Pinatti <[EMAIL PROTECTED]> > wrote: > > > Hello, > > > > > > About the syntax, I meant the second one, actually, declare the > _instance > > > just in constructor, but I just noticed a very weird thing...When I get > > null > > > as I was referencing, if I just declare in my main class, a var casting > > the > > > type of class that was returning null, it'll work. Seems compiler "see" > > that > > > the class exists, and so it returns what I expect. It's really weird, > > since > > > the singleton pattern is still the same. If i don't declare this dummy > > var > > > with the type I want, even I'm sure the instance exists, it returns > null. > > I > > > really don't know why this is happening, but it was the workaround I've > > > found. > > > > > > About the singleton implementation, I agree with you isn't the best > thing > > to > > > do, but in my case, I'm using that for 3 document classes, that for > sure > > > I'll use just one single time, so, it's the quickest solution I guess. > > > > > > And very nice your approach about minimize Singleton use. I'll take a > > try, > > > it can save a lot of time. > > > > > > If you got my problem (i know it's a quite mess to explain), would be > > great > > > know why this happens. Maybe it's a Gaia thing that I don't know how to > > use, > > > but I was wondering if it was being a stupidity of mine with singleton > > > concept, since I'm a bit new with oop. > > > > > > Thanks a lot, > > > Pinatti > > > > > > On Fri, Nov 21, 2008 at 10:53 AM, Hans Wichman < > > > [EMAIL PROTECTED]> wrote: > > > > > >> Hi, > > >> > > >> im not sure about your syntax. > > >> > > >> This: > > >> > > >> class MyClass { > > >> > > >> public static var _instance:MyClass = this; > > >> > > >> > > >> } > > >> > > >> will not work. > > >> > > >> You are looking for something like: > > >> > > >> class MyClass { > > >> > > >> public static var _instance:MyClass = null; > > >> > > >> public function MyClass () { > > >> _instance = this; > > >> } > > >> } > > >> > > >> Although I have to add this is the most rudimentary and worst > > >> implementation I can come up with, but it will work. > > >> It will allow public access to your instance variables and overwrite > > >> any instance already there when you create a page of the same class as > > >> well :). > > >> > > >> As an another example how cool my application registry that I posted > > >> about yesterday is :)) (shameless self promotion), imagine doing this > > >> in each page: > > >> > > >> _application.register (this); > > >> > > >> and when you need a page you do: > > >> > > >> HomePage(_application.getRegistree(HomePage)) > > >> > > >> if you need to get all pages you do: > > >> > > >> pageArray = _application.getRegistrees (AbstractPage) (or page can't > > >> recall the gaia page superclass at the moment). > > >> > > >> You can make the _application a singleton if you wish Application.etc > > >> or you can call it PageRegistry or whatever, the idea remains the > > >> same. This will reduce the amount of singletons by far. It also shows > > >> how easy it is to integrate this powerfull approach into whatever > > >> workflow/framework you work with. > > >> > > >> regards, > > >> JC > > >> > > >> On Fri, Nov 21, 2008 at 1:06 PM, Fabio Pinatti <[EMAIL PROTECTED]> > > wrote: > > >> > Hello list, > > >> > > > >> > I've been having a question since a time ago... I'm using a flash > > >> framework > > >> > within my websites (Gaia), and between the classes, I use some > > singleton > > >> > pattern to communicate between them. This way, theorically, I can > get > > any > > >> > class instance from another, and I don't need to know the path for > > each > > >> > instantiated class on stage. To clarify, imagine I have a home and a > > form > > >> > page. I could get the reference for home page from form page, just > > >> setting > > >> > up a static var: > > >> > > > >> > var _home:HomePage = this; > > >> > > > >> > and through a singleton, I could get a HomePage._home, from any > point > > of > > >> my > > >> > application, right? > > >> > > > >> > The point is, for some classes, that works and returns the class > > >> instance. > > >> > For other, that doesn't works and returns null, even I'm sure the > > class > > >> is > > >> > with the _home var setted up. I posted this question here, because > I'm > > >> > wondering if this can be some problem (of mine) with Singleton > > pattern, > > >> and > > >> > not with framework. Why does Singleton sometimes works, and other > > times, > > >> > doesn't? Did you have some similiar problem getting classes > instances, > > >> like > > >> > me? > > >> > > > >> > Thanks, and sorry if it's a really stupid question. > > >> > > > >> > Kind Regards, > > >> > > > >> > -- > > >> > Fábio Pinatti > > >> > :: web.developer > > >> > :::: www.pinatti.com.br > > >> > :::::: 19. 9184.3745 / 3342.1130 > > >> > _______________________________________________ > > >> > Flashcoders mailing list > > >> > Flashcoders@chattyfig.figleaf.com > > >> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > >> > > > >> > > >> _______________________________________________ > > >> Flashcoders mailing list > > >> Flashcoders@chattyfig.figleaf.com > > >> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > >> > > > > > > > > > > > > -- > > > Fábio Pinatti > > > :: web.developer > > > :::: www.pinatti.com.br > > > :::::: 19. 9184.3745 / 3342.1130 > > > _______________________________________________ > > > Flashcoders mailing list > > > Flashcoders@chattyfig.figleaf.com > > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > > > _______________________________________________ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > > -- > Fábio Pinatti > :: web.developer > :::: www.pinatti.com.br > :::::: 19. 9184.3745 / 3342.1130 > _______________________________________________ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > _______________________________________________ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders