I help understanding singleton instance

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

ssmacwilliamsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
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
zzynxSoftware engineerCommented:
>> I try and create the default constructor but it causes error
It's always handy if you tell us WHAT error ;o)
0
ssmacwilliamsAuthor Commented:
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

zzynxSoftware engineerCommented:
>> 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
ssmacwilliamsAuthor Commented:
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
zzynxSoftware engineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ssmacwilliamsAuthor Commented:
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
zzynxSoftware engineerCommented:
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
ssmacwilliamsAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Flex

From novice to tech pro — start learning today.