all groups > flash actionscript > september 2005 >
You're in the

flash actionscript

group:

Class member access and function declaration.


Class member access and function declaration. Blade alSlayer
9/17/2005 10:26:49 AM
flash actionscript:
Hi there!
Question 1:
I use member access control a lot in C++ and I try to do so in Flash as well.
But there seems to be a problem:
Suppose we have a simple class:
class memberAccessTest{
public var pub:Number;
private var priv:Number;
}

and we create an instance of it in a fla file:
var test:memberAccessTest = new memberAccessTest();
test.priv = 5; // generates compiler error
test.pub = 6; // OK

Now this is ok - but lets see what happens if the user forgets to strictly
type the instance:

var test2 = new memberAccessTest();
test2.pub = 5; // OK
test2. priv = 6; // also OK?!?!?

Now as a develover you can declare private and public members (it is not good
users to have access to some internals of a class), but you CANNOT force the
user to strictly type the instances of your class... is there workaround?

Question 2:
In Flash MX 2004 Pro help files (and in the online help too) in the artice
http://livedocs.macromedia.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/
wwhelp.htm?context=Flash_MX_2004&file=00003105.html it is said that when you
extend the UIObject class you should declare the dispatchEvent function before
you call it like this:
private var dispatchEvent:Function;
Now this seems not to be right - I created multiple test classes and even when
using strict data typing EVERYWHERE the compiler didn't return any error when
this function was not declared in my class definition. Any ideas on this one?
And should methods that are inherited from a superclass be declared again in
the derivative class in order to be used? As far as my experiments go - they
should not...

Thank you in advance...
Re: Class member access and function declaration. LuigiL
9/18/2005 12:00:00 AM
Well, it gets even weirder because the following will be OK too:
var test2 = new memberAccessTest();
test2.pub = 5; // OK
test2. priv = 6; // also OK?!?!?
test2.testing = 11; // yep, also OK
So, dynamically adding a property 'testing' is allowed too, even when our
class is not defined as dynamic.

And all is solved by 'strict' datatyping our variables. Unlike Java (for
instance) the compiler can't tell us if the user omits the datatype so... yep,
the user has to use var test2:memberAccessTest=new memberAccessTest(); to get
the compiler to warn us in case we have an error in our code. By comparison, in
Java, the code won't compile if the datatype is omitted.
A work around? Don't think so.

Question 2:
I guess it would depend on your code, so without seeing any code... I can't
help you with this one.

Declaring methods again in subclasses: no, not if you don't override them.
Re: Class member access and function declaration. Ganesh GV
9/19/2005 8:13:21 AM
Hi

Re: Class member access and function declaration. LuigiL
9/19/2005 8:23:35 AM
Because your class extends UIObject it has access to its properties (also the
private ones!) and methods. UIObject already has a property var
dispatchEvent:Function so your subclass works without declaring the property.

The metadata are added in case you are creating a component and are the
parameters that can be set by a developer in a panel in the authoring
environment.
Re: Class member access and function declaration. Blade al'Slayer
9/19/2005 10:35:17 AM
Thank you very much!
As for code example for my second question - here goes something:

class test extends UIObject{
// var dispatchEvent:Function;
// var addEvetListener:Function;
// the above should be placed according to the manual
// but even commented out it works
function testFunction (){
dispatchEvent({type:"some_event", target:this});
}
}

In FLA file we create an instance:
var t:test = new test();
t.testFunction(); // event is dispatched (in my code I detect that, by
doing a little modification to the EventDispatcher.as source file to
trace captured events)
Note: Event dispatching is a feature of the UIObject class byt yet not
inherited directly. The UIObjectExtensions class must be instantiated
too, because it is where the EventDispatcher.initialize() method is
called. Or you can call that method yourself, but thus removing any
other "extensions" that the UIObjectExtension class offers. SO the best
is to place an instance of UIObject in the lybrary (it comes packed will
the other one...)
--
Re: Class member access and function declaration. Blade al'Slayer
9/19/2005 10:37:42 AM
And just one more thing:
The help files say you must place event metadata tags, but does not
explain why you need them. The compiler needs them for some reason, but
does that mean events won't work if you don't place the metadata?

--
Re: Class member access and function declaration. Blade al'Slayer
9/20/2005 12:00:00 AM
Thank you!

--
AddThis Social Bookmark Button