Solved

I help understanding singleton instance

Posted on 2009-07-15
9
479 Views
Last Modified: 2012-05-07
I have a value object listed below, but I can't get it to work.  Could someone please explain why and assist me in finding the solution? I try and create the default constructor but it causes error
package components.vo

{

     import flash.events.Event;

		

     public class User_vo extends Event

          {

	//This is the USER table structure in order from the query

	public var USER_ID:int;

                     public var ACCESS_LEVEL:String;

                     public var SC_ID:int;

                     public var UNAME:String;		

                     private static var _instance : User_vo;

                     public var CurrentUser:Object;

        

	//This will set the current user info       

            

	  /**   * Getter for the singleton instance        

	      public static function getInstance():User_vo

		      {

	      		 var CurrentUser:Object;

	      		 var type:String;

		         if (_instance==null || _instance==undefined)

		               _instance = new User_vo(type, CurrentUser);

		          return _instance;

		      }            */ 
 

	public function User_vo(type:String, CurrentUser:Object)

	     {

	         super(type);

	        this.USER_ID=CurrentUser.USERID;

	        this.ACCESS_LEVEL=CurrentUser.ACCESS_LEVEL;

	        this.SC_ID=CurrentUser.SC_ID;

	        this.UNAME=CurrentUser.UNAME;

	     }

						

	public function getServiceCenter():int

	      { return this.SC_ID	;	}

	public function getAccessLevel():String

	      { return this.ACCESS_LEVEL	;	}		public function getUserName():String

	      { return this.UNAME	;	}		

	public function getUserId():int

	     { return this.USER_ID	;	}								

	}

}

Open in new window

0
Comment
Question by:ssmacwilliams
  • 5
  • 4
9 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
I think your class User_vo should have these:


          // The sole instance of this (singleton) class
        private static var _instance : User_vo;      


          // Getter for the singleton instance        
          public static function getInstance():User_vo {
                      return _instance;
          }
         
          public static function set instance(inst:User_vo):void {
                  _instance = inst;
          }

          // Empty constructor
          public function User_vo() {
          }



For filling up the instance:

    var _user_vo : User_vo = new User_vo();
    _user_vo.type = ...;            // assuming you also have the decent setter functions for this
    _user_vo.USER_ID = ...;
    _user_vo.ACCESS_LEVEL = ...;
    _user_vo.SC_ID = ...;
    _user_vo.UNAME = ...;
    User_vo.setInstance(_user_vo);

After this you can use it whereever you want as:

   x = User_vo.getInstance().type;
or
   x = User_vo.getInstance().userId; // assuming you also have the decent getter functions for this
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> I try and create the default constructor but it causes error
It's always handy if you tell us WHAT error ;o)
0
 

Author Comment

by:ssmacwilliams
Comment Utility
Sorry about that,
If I uncomment out the code, I get the following WARNING.
     1012: Variables of type User_vo cannot be undefined. The value undefined will be type coerced to User_vo before comparison.

But in Runtime...
TypeError: Error #2007: Parameter type must be non-null.
      at flash.events::Event()
      at components.vo::User_vo()[C:\Documents and Settings\JBG\My Documents\FSMO\RMS\src\components\vo\User_vo.as:29]
      at components.vo::User_vo$/getInstance()[C:\Documents and Settings\JBG\My Documents\RMS\src\components\vo\User_vo.as:23]

Using this syntax to call it...
    Alert.show(User_vo.getInstance().getServiceCenter().toString());

My thoughts were when I was calling it I was passing it an undefined value.. So I added the to the commented code the undefined part and added the vars.  If I didn't add the them I would get some error of having 2 functions by the same name.  I don't think flex likes polymorphism. :)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> If I uncomment out the code, I get the following WARNING.
>>     1012: Variables of type User_vo cannot be undefined. The value undefined will be type coerced to User_vo before comparison.
You solve that by replacing

    if (_instance==null || _instance==undefined)

by just

    if (_instance==null)


>> But in Runtime...
>> TypeError: Error #2007: Parameter type must be non-null.
That's because
                      var CurrentUser:Object;
                      var type:String;
are (still) null when you pass them to the constructor:

                      _instance = new User_vo(type, CurrentUser);

and in the constructor you have:

        super(type)

which is the constructor of Event and that one doesn't accept a null as parameter.
And even if it would the next line in your constructor

        this.USER_ID=CurrentUser.USERID;

would result in a nullpointer since CurrentUser is null.


Did you try what I suggested?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ssmacwilliams
Comment Utility
Sorry about the time delay...
 Ok, below is the code modifed. I am still getting the error:
       
      1203: No default constructor found in base class flash.events:Event.

Correct me if my understanding is flawed in the comments of each line.  
 
Now what is missing is passing those values to this user_vo and then assigning the value to the variables:
I dispatch an event that passes the values to the User_vo. via the following code:
     var UserInfo:Object= new Object();
     //populate the User vo for the rest of the application
     UserInfo.USER_ID=CurrentUser[0].USER_ID;
     UserInfo.ACCESS_LEVEL=CurrentUser[0].RMS.toString();
     UserInfo.SC_ID=CurrentUser[0].SC_ID;
     UserInfo.UNAME=CurrentUser[0].FNAME.toString();
                              
    //write values to the user object      
    var SelectedData:User_vo= new User_vo("active_user", UserInfo);
    dispatchEvent(SelectedData);            

You made the suggestion of filling the instance:
    var _user_vo : User_vo = new User_vo();
    _user_vo.type = ...;            // assuming you also have the decent setter functions for this
    _user_vo.USER_ID = ...;
    _user_vo.ACCESS_LEVEL = ...;
    _user_vo.SC_ID = ...;
    _user_vo.UNAME = ...;
    User_vo.setInstance(_user_vo);

Where would this be done?
Thank your for your patience.
I have no quams about breaking this into another question for your time/points. It is worth the insight and critic.
package components.vo

{

     import flash.events.Event; //Importing class Events

		

      public class User_vo extends Event //Extending class so I can inherit/access its methods/properties

	{

	  //I define all of the fields that I will be passing to other objects,etc

 	  public var USER_ID  	:int;

           public var ACCESS_LEVEL 	:String;

           public var SC_ID    	:int;

           public var UNAME    	:String;		

           public var CurrentUser:Object; 

                //Create an object to contain all 

                //the values passed to/from this object

           private static var _instance :User_vo; 

                //Not sure but guessing that this is 

                //creating the instance of the User_vo to 

               // hold until called

        

          public function User_vo() // Create default Empty constructor

            {  }		      

   

	 public static function getInstance():User_vo 

	   {  return _instance;      } 

               //function that returns current instance of User_vo
 

		               

          public static function set instance(inst:User_vo):void 

            {   _instance = inst;    }

          	//function called from outside this class to set the User_vo			 

	public function getServiceCenter():int

	    { return this.SC_ID	;	}

	     //function called to get the SC_ID

         public function getAccessLevel():String

	    { return this.ACCESS_LEVEL	;	}		    //function called to get the ACCESS_LEVEL

	public function getUserName():String

	    { return this.UNAME	;	}

	   //function called to get the UNAME

	public function getUserId():int

	    { return this.USER_ID	;	}

         //function called to get the USER_ID	

	}

}

Open in new window

0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
Comment Utility
I don't understand why you would pass this data via an event.

This is OK:
>> var UserInfo:Object= new Object();
>>     //populate the User vo for the rest of the application
>>     UserInfo.USER_ID=CurrentUser[0].USER_ID;
>>     UserInfo.ACCESS_LEVEL=CurrentUser[0].RMS.toString();
>>     UserInfo.SC_ID=CurrentUser[0].SC_ID;
>>     UserInfo.UNAME=CurrentUser[0].FNAME.toString();

This part I don't understand:
>>    //write values to the user object      
>>    var SelectedData:User_vo= new User_vo("active_user", UserInfo);
>>    dispatchEvent(SelectedData);      

Try replacing it by just this:

       User_vo.setInstance( new User_vo("active_user", UserInfo) );


Now wherever (ie. in whatever class) you need the userId you can write:

var userId:int = User_vo.getInstance().getUserId();

or

var accessLevel:String = User_vo.getInstance().getAccessLevel();


So, I don't see the need to extend from Event:
public class User_vo {
  ....
}
0
 

Author Closing Comment

by:ssmacwilliams
Comment Utility
You have answered the original question in explaining the why and finding a solution, I will be adding another question for the 'finessing' of the solution. Thanks.... btw...after reviewing the code I couldn't see why I need to pass it as an event either
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanx 4 axxepting

>> I will be adding another question for the 'finessing' of the solution.
Feel free to draw my attention on it by posting the URL of it in this question.
0
 

Author Comment

by:ssmacwilliams
Comment Utility
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now