PDA

View Full Version : Accessing Dictionary Object from Function



Pherank
October 23rd, 2008, 11:04 PM
I'm trying to figure out how I can access the keys/data within a Dictionary object from within a public function. The basic code structure looks like this:



package {

import flash.utils.Dictionary;

public class TriviaGame extends MovieClip {

public var dict:Dictionary = new Dictionary(true);

public function askQuestion() {
var answerField:TextField = createText(answer,answerFormat,answerSprite,0,0,45 0);
dict[answerField] = answerField;
trace("dict[answerField] = " + dict[answerField].text);// Gives correct data
}
public function clickAnswer(event:MouseEvent) {
finishQuestion();
}

public function finishQuestion() {
trace(dict[answerField]); //Error 1120: Access of undefined property answerField.
}

}

}
I thought that the Dictionary would be accessible throughout the public class but that doesn't seem to be so. I don't understand why the key becomes undefined when accessed from another function. What do I need to do to reach "dict" from within the finishQuestion() function?

theCodeBot
October 23rd, 2008, 11:06 PM
answerField is not defined outside of your constructor. The dictionary uses objects of keys, if those objects are innaccessible or undefined, then you can't access the dictionary. You could, however, use an object and use the STRING "answerKey" as the index instead of the actual object. This is much better for what you're trying to do.



var objs:Object = {"answerKey":answerKey};
trace(objs["answerKey"]); //[object Object] -- the actual answerKey

Pherank
October 23rd, 2008, 11:26 PM
I'm getting the same error though - "answerKey" is undefined

1120: Access of undefined property answerKey. As you say, outside the constructor I can't reference the data. So what's the technique for dealing with this?

Btw, I though it was supposed to be easier to use the Dictionary Class to store objects.

theCodeBot
October 24th, 2008, 03:20 PM
I'm getting the same error though - "answerKey" is undefined

1120: Access of undefined property answerKey. As you say, outside the constructor I can't reference the data. So what's the technique for dealing with this?

Btw, I though it was supposed to be easier to use the Dictionary Class to store objects.

What you're looking for is an object rather than a dictionary, like I said. A dictionary uses an actual object as the references to the data. An object, however, uses strings to reference it's data. You're looking for the Object. Look at this:


//Look here:
var answerKey:MovieClip = new MovieClip();
var keyDict:Dictionary = new Dictionary();
var keyObj:Object = new Object();
keyDict[answerKey] = answerKey;
//if you lose track of answerKey the object, yuou can't access it's dictionary reference!
keyObj["answerKey"] = answerKey;
//Notice the quotes. This is a string literal, it can't change.
//This reduces redundancy and stops this error you're having.
keyDict[someMovieCliponStage] = "Something about the clip on stage";
//That's what dictionaries are for.
keyObj["someWord"] = someMovieClipOnStage;
//That's what objects are for.