View Full Version : Extended class needs to communicate with parent class - How?
waffe
June 14th, 2008, 09:20 PM
Hi,
I am working with an extended class that has a text event handler in it, all of this works perfectly but I need to have communication with classes that extends this class. How is this done?
Example:
ContentView extends BasicContentView
How can BasicContentView trace a variable in ContentView? Can this be done?
In the BasicContentView I do a trace(parent) and it reads ContentView but If I try and read a variable in Content view I get this error:
1119: Access of possibly undefined property tempString through a reference with static type flash.display:DisplayObjectContainer.
Thanks,
waffe
Fidodo
June 14th, 2008, 09:45 PM
So you want to access a variable that is declared in ContentView from BasicContentView? No you can't do that because all BasicContentView knows about is what it extends and imports, and what is in that class. But why do you need to do this? There's probably a better way to do that. If you show me the code that your trying to implement I can help you better.
waffe
June 14th, 2008, 10:18 PM
That makes sence from your post and from reading up alittle on the topic. I can do it by removing the TextFeild code out of the BasicContentView and into each class that needs it. I just like grouping up code that I need more than once. Maybe you can show me a better a way. - Thanks
Here is a simplified version of the code I am working with:
package sbTestSide{
import flash.display.Sprite;
internal class TestTypeView extends BasicContentView{
public function TestTypeView() {
createTextFormat(aTextFormateProp[0], aTextFormateProp[1], aTextFormateProp[2], aTextFormateProp[3], "Type a question here", "QuestionTB");//x, y, width, height, text
}
}
}
package sbTestSide{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFieldType;
import flash.text.TextFormatAlign;
import flash.events.*;
internal class BasicContentView extends Sprite {
internal var tText:TextField;
internal var qText:TextField;
public function BasicContentView() {
//
}
//Create Text Field ------------------------------------------------------------------------------------------
protected function createTextFormat(xLoc:Number, yLoc:Number, wWidth:Number, hHeight:Number, sText:String, sTBName:String):void{
qText = new TextField();
qText.type = TextFieldType.INPUT
qText.x = xLoc;
qText.y = yLoc;
qText.width = wWidth;
qText.height = hHeight;
qText.selectable = true;
qText.border = false;
qText.wordWrap = true
qText.multiline = true;
qText.tabEnabled = true;
qText.addEventListener(Event.CHANGE, textChanged);
qText.name = sTBName;
//qText.autoSize = TextFieldAutoSize.LEFT;
var Format:TextFormat = new TextFormat();
Format.font = "Arial";
Format.color = 0x00000;
Format.size = 12;
qText.defaultTextFormat = Format;
qText.text = sText;
addChild(qText);
//Event-------------------------------
function textChanged(event:Event):void{
trace(event.target.text);
if(qText.name == "QuestionTB"){
//** This is the line that I need to run**
//parent.parent.parent.wsWebService.setXMLTestQuesti ons(event.target.text)
trace("1");
}else if (qText.name == "AnswerTB"){
trace("2");
}else if (qText.name == "YourAnswerTB"){
trace("3");
}
trace("qTextName= "+qText.name);
}
}
}
}
hexvector
June 14th, 2008, 11:17 PM
You can also set the reference to your field in the package and that way the classes in the package may reference it.
waffe
June 14th, 2008, 11:19 PM
Sounds good hexvector, but could you explain that alittle more?
Fidodo
June 14th, 2008, 11:20 PM
You don't declare anything in TestTypeView, so what could you possibly reference from BasicContentView that is in TestTypeView? Anyways, if you want to access a property from the parent class you should declare it in the parent class since it will be unanimous with all classes that extend it right? If it's specialized to the class you need to have class specific code to deal with it.
hexvector
June 14th, 2008, 11:34 PM
Sounds good hexvector, but could you explain that alittle more?
Actually I don't think it works I must be imagining things. I thought I read somewhere about a package scope but I believe that is only for import statements being global for classes within the package.
waffe
June 15th, 2008, 01:40 AM
Thanks people, I get what I need to do...
waffe
amarghosh
June 16th, 2008, 01:19 AM
package scope refers to internal scope modifier. if u declare a class level variable without any modifier (public, private or protected), it falls to the default scope internal. internal variables/methods are accessible only by the classes in the same package and not outside of it.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.