ssmacwilliams
asked on
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 ; }
}
}
>> I try and create the default constructor but it causes error
It's always handy if you tell us WHAT error ;o)
It's always handy if you tell us WHAT error ;o)
ASKER
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 :\Document s and Settings\JBG\My Documents\FSMO\RMS\src\com ponents\vo \User_vo.a s:29]
at components.vo::User_vo$/ge tInstance( )[C:\Docum ents and Settings\JBG\My Documents\RMS\src\componen ts\vo\User _vo.as:23]
Using this syntax to call it...
Alert.show(User_vo.getInst ance().get ServiceCen ter().toSt ring());
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. :)
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
at components.vo::User_vo$/ge
Using this syntax to call it...
Alert.show(User_vo.getInst
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. :)
>> 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.U SERID;
would result in a nullpointer since CurrentUser is null.
Did you try what I suggested?
>> 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.U
would result in a nullpointer since CurrentUser is null.
Did you try what I suggested?
ASKER
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=CurrentUs er[0].USER _ID;
UserInfo.ACCESS_LEVEL=Curr entUser[0] .RMS.toStr ing();
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.
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=CurrentUs
UserInfo.ACCESS_LEVEL=Curr
UserInfo.SC_ID=CurrentUser
UserInfo.UNAME=CurrentUser
//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_
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
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
>> 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.
// 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):voi
_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_
After this you can use it whereever you want as:
x = User_vo.getInstance().type
or
x = User_vo.getInstance().user