PDA

View Full Version : Using vars across classes



nikomax
April 25th, 2008, 08:43 PM
Hi

How do I /can I access variables from other classes.

Basically my document class creates the variables and my imported classes uses their values.

Also can an imported class in my document class, send variables to the document class that can be accessed from a different imported class

AND is there a much better why of doing this xD

Thanks

EDIT::

I got thinking that this might seem very basic. I have been reading up on class encapsulation for a while now and although I think be passing my variables a public any class should be able to access them, I don't know how to do it. Mainly because I cant find an example

thatguythatsme
April 26th, 2008, 04:29 AM
I'm not an expert on OOP but I know most (both) books I've read on OOP give examples of using getter and setter methods to expose class properties. I was then shown by a C# developer that using getters and setters all over the shop is not the way to build applications. Found a really good article on this here as well by Darron Schall who's an excellent actionscript developer.

http://www.darronschall.com/weblog/archives/000149.cfm

He also gives examples of using both getters and setters vs public variables.

Hope this helps

Dave

dzhedzho
April 26th, 2008, 06:37 AM
About getters and setters...
AS3 does not support property overriding, therefore using getters and seters is stated as good practice regarding AS3.


package classPack{
public class SomeClass {
var var0; //internal varible - visible to all classes of the same package
private var var1; //private variable - visible only to the class that defines it
protected var var2; //protected var - visible only to the class that defines it and to any subclasses of that class.
public var var3; //public var - visible to all

//gives "public access" to var0, since there is only getter, public access is read only
public function get varOne () {
return (var1);
}
}
}


on the originaal question,
as a best practice, you should pass refrence to those variables to the classes that use them.
In general its a bad practice of child inctance messing up with its parent varibles.
This is somehow new to AS3

In AS2 you would do something like


var someVar:Number = 20;
btn.onPress = function () {
trace(this._parent.someVar)
}

and that will be totally fine
In AS3 the child will invoke "parent" method to pass that value

I don't know the case you are working on and what you try to acheve, but in general the parent should pass the required data to the children, and not the children just getting it when they feel like it. Sorry I can't be more help, if you explain what you are trying to acheve, maybe we can help you better...

nikomax
April 26th, 2008, 07:46 AM
In reply to dzhedzho:

Just to clarify, using getters ans setters can any variable in use, as long as its public, be used in any class? I will try this soon, but I thought I would just ask.

Also:

s a best practice, you should pass refrence to those variables to the classes that use them.
In general its a bad practice of child inctance messing up with its parent varibles.
This is somehow new to AS3

In AS2 you would do something like

var someVar:Number = 20;
btn.onPress = function () {
trace(this._parent.someVar)
}


and that will be totally fine
In AS3 the child will invoke "parent" method to pass that value
In not 100% sure what you mean here (sorry xD)

Trying to explain things a bit more::

I have XML that is being loaded in a class imported to my document class,
also my code for my GUI is also imported to my document class,

How do I get the values from the XML class to the GUI class.?

I was wondering if I could pass the variables from the XML class to the document class, then on to the GUI class.

right now using a getter sounds like it will work, and I will try it, but is there another way?

ptfury
April 26th, 2008, 03:07 PM
search for uza.utils i use it to create global vars like as2