PDA

View Full Version : static hash array


firestar
12-08-2003, 11:36 PM
Hi,

I am working on a Error Message Class, SeErrorMsg, that will allows me to read the error number and error msg from a text file and into a static hash array. The error number will be the "Key", and the error msg will be the "Value". I seem to have no problem of referencing the hash array, since I am able to print out the error msg by stating the following the inside the static function,initialized() , of the Error Message Class: trace("_aErrorMsg["+sKey+"]= " + _aErrorMsg[sKey]);

However, when I reference the hash arrary from the first frame of my errorTest.fla or inside a diffrent class in the following manner: trace(SeErrorMsg._aErrorMsg["e0001"]);

The trace display "undefined". I am baffled about this issues, I think it might be a timing issue, please help me on how to resolve it.

Please see my Error Class,
// Define Error Message class SeErrorMsg
// In SeErrorMsg.as:
class com.abc.seerror.SeErrorMsg {
static var _aErrorMsg:Array = new Array();
//static var _aErrorMsg:LoadVars = new LoadVars();
//constructor
function SeErrorMsg() { }

static function initialized():Boolean{
var loadSeErrMsg:LoadVars = new LoadVars();
var locale:String = "us";
//var path:String = "";
loadSeErrMsg.load(locale+"SeErrMsg.txt");
loadSeErrMsg.onLoad = function(success){
var obj:LoadVars = new LoadVars();
obj = this;
var sKey:String = "";
var sValue:String = "";
if (success) {
for (var properties:String in obj) {
if (properties != "onLoad" && properties != undefined) {
sKey = properties;
// trace ("skey " + sKey instanceof String);
sValue = obj[properties];
setErrMsg(sKey, sValue);
// trace("_aErrorMsg["+sKey+"]= " + getErrMsg(sKey));
trace("_aErrorMsg["+sKey+"]= " + _aErrorMsg[sKey]);
}
} // end of for loop
} else {
trace ("not loaded");
} // end of if(success)
} //end of onload

return true;
/**
_aErrorMsg["0001"] = "Invalid Email";
_aErrorMsg["0002"] = "The field name(s) can't be left empty";
_aErrorMsg["0003"] = "Please enter the password";
_aErrorMsg["0004"] = "Database connection error";
**/
}

static function setErrMsg(sEnum:String, sEmsg:String) {
_aErrorMsg[sEnum] = sEmsg;
}

static function getErrMsg(sEnum:String):Array {
return _aErrorMsg[sEnum];
}

static function get_aErrMsgs():Array {
return _aErrorMsg;
}
}
Here is my code in testErro.fla
import com.abc.seerror.*;
// Function: main()
// Desc: The single entry point for the application
var _aErrorMsg:Array = new Array(this);
SeErrorMsg.initialized();
_aErrorMsg = SeErrorMsg.get_aErrMsgs();
//trace(SeErrorMsg._aErrorMsg["e0001"]);
trace(_aErrorMsg["e0001"]);
for (var sKey:String in _aErrorMsg) {
trace(_aErrorMsg[sKey]);
}
Here is the content of my text file, usSeErrMsg.txt
e0001=Invalid Email&e0002=Please enter password

Thanks for your help




Please use the proper code syntax to format the code.
Read this page (http://www.actionscript.org/forums/misc.php3?action=bbcode#buttons) to find out more about the tag information.
Using this feature will assist people in reading and understanding your code and hopefully help you get better assistance. Best of all, it's easy to use; all you have to do is enter your code between a [ as ] tag and a [ /as ] tag. For example, try [ as ]trace("This is a test");[ /as ](Get rid of the space between the open/close brackets('[' and ']')

ericlin
12-09-2003, 05:48 AM
I think it might be a timing issue
That is right. It is a timing issue.

In the last line of testErro.fla, write a script:
trace("testErro"); Then run the movie. You will see that, the "loaded" is trace out later than "testErro".

BTW, your _aErrorMsg should be an Object not an Array.

firestar
12-09-2003, 01:24 PM
Thanks, for the quick reply. However, would you please help me on how to resolve this timing issues?

Thanks,

firestar

ericlin
12-10-2003, 12:07 AM
If you give it some time to load, it will work.

For example:make a button "bt"

import com.smartequip.seerror.*;
// Function: main()
// Desc: The single entry point for the application
var _aErrorMsg:Array = new Array(this);
SeErrorMsg.initialized();
bt.onRelease = function() {
_aErrorMsg = SeErrorMsg.get_aErrMsgs();
//trace(SeErrorMsg._aErrorMsg["e0001"]);
trace(_aErrorMsg["e0001"]);
for (var sKey in _aErrorMsg) {
trace(_aErrorMsg[sKey]);
}
};


However, when on line, if user click the button before the loadVars completes the loading, it fails.

You can adopt the technique of pre-loader, when load, jump to main frame or something.

firestar
12-12-2003, 07:14 PM
Hi Eric,
Thanks for your suggestion on the preloader, I have tried it in my testError.fla file. It does work. However, I need to access the static Hash Arrary from my other Classes like Application Error Class, Database Error Class, System Error Class...etc. These Classes are ".AS" Files. Therefore, I can't used the technique of preloader.
Although, I can reference the hash array within the testError.fla after applying the preloader technique. But when I doese the followin inside the testError.fla
try {
throw new ApplicationError("e0002");
} catch (e:ApplicationError) {
trace (e.getErrMsg());
}

It seems when I instantiate the DatabaseError Class inside the my testError.fla. The static Hash Array is still not defined within my DatabaseError Class. Therefore, I can't access the hash array from DatabaseError Class. Is there a way to resolve this timing issue? or should I get make the hash array into global, _global._aErrorMsg, since I need to access the error msg from Hash Array within most of my classes.