Re: [Flashcoders] singleton returns null

2008-11-24 Thread Fabio Pinatti
I thought it was too (didn't check the existence of _instance), but I was
sure the class was initialized and _instance != null, since it's a document
class, and even after I changed the code, it keeps giving me the error, and
the really weird thing is, it works if I declare a dummy variable, with the
type of class that returns null.. In my example, if I put a private var
_it:SecondClass, the _instance that was null, now returns a valid class
instance.. I'm starting thinking about that it can be a framework thing, and
not the singleton syntax, that is really simple..

Thanks for all, anyway!
Pinatti


On Fri, Nov 21, 2008 at 5:21 PM, Ricky Blaha [EMAIL PROTECTED] wrote:

 Fábio,

 The reason you are seeing null is because your getInstance() does not
 assign
 the instance of SecondClass to _instance if it hasn't already been
 instantiated:

   public static function getInstance():SecondClass {
  return _instance; //_instance has never been assigned
 anything
   }

 vs.

   public static function getInstance():SecondClass {
  if (_instance == null)
   _instance = new SecondClass();
 return _instance;
   }


 ~Ricky
 http://codingjourney.blogspot.com


 On Fri, Nov 21, 2008 at 12:49 PM, 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 

Re: [Flashcoders] singleton returns null

2008-11-24 Thread Paul Andrews

Fabio,

It would be better to sort out what is happening. If you can post the 
singleton class code that you are using, together with your simple test 
case, that would be good.  Ricky's advice should be correct.


Paul
- Original Message - 
From: Fabio Pinatti [EMAIL PROTECTED]

To: Flash Coders List flashcoders@chattyfig.figleaf.com
Sent: Monday, November 24, 2008 1:05 PM
Subject: Re: [Flashcoders] singleton returns null


I thought it was too (didn't check the existence of _instance), but I was
sure the class was initialized and _instance != null, since it's a document
class, and even after I changed the code, it keeps giving me the error, and
the really weird thing is, it works if I declare a dummy variable, with the
type of class that returns null.. In my example, if I put a private var
_it:SecondClass, the _instance that was null, now returns a valid class
instance.. I'm starting thinking about that it can be a framework thing, and
not the singleton syntax, that is really simple..

Thanks for all, anyway!
Pinatti


On Fri, Nov 21, 2008 at 5:21 PM, Ricky Blaha [EMAIL PROTECTED] wrote:


Fábio,

The reason you are seeing null is because your getInstance() does not
assign
the instance of SecondClass to _instance if it hasn't already been
instantiated:

  public static function getInstance():SecondClass {
 return _instance; //_instance has never been assigned
anything
  }

vs.

  public static function getInstance():SecondClass {
 if (_instance == null)
  _instance = new SecondClass();
return _instance;
  }


~Ricky
http://codingjourney.blogspot.com


On Fri, Nov 21, 2008 at 12:49 PM, 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

Re: [Flashcoders] singleton returns null

2008-11-21 Thread Hans Wichman
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


[Flashcoders] singleton returns null

2008-11-21 Thread Fabio Pinatti
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


Re: [Flashcoders] singleton returns null

2008-11-21 Thread Fabio Pinatti
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


Re: [Flashcoders] singleton returns null

2008-11-21 Thread Hans Wichman
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


Re: [Flashcoders] singleton returns null

2008-11-21 Thread Fabio Pinatti
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, 

Re: [Flashcoders] singleton returns null

2008-11-21 Thread Todd Kerpelman
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 

Re: [Flashcoders] singleton returns null

2008-11-21 Thread Ricky Blaha
Fábio,

The reason you are seeing null is because your getInstance() does not assign
the instance of SecondClass to _instance if it hasn't already been
instantiated:

   public static function getInstance():SecondClass {
 return _instance; //_instance has never been assigned
anything
   }

vs.

   public static function getInstance():SecondClass {
 if (_instance == null)
   _instance = new SecondClass();
 return _instance;
   }


~Ricky
http://codingjourney.blogspot.com


On Fri, Nov 21, 2008 at 12:49 PM, 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