-
Senior Member
check if objects's X Class implements interface Y
Hi
I have an object(myObject) which is an instance of a class (myClass), I would like to make sure that myClass implements a certain interface (myInterface).
In other words: I have to browse through an array of objects and invoke a certain method in the ones that have such method ( the method is listed in myInterface).
Thanks in advance
LeoBeer
-
You can use interfaces as datatypes, which means you can test whether an object implements one by using instanceof...
In this example, lets say you have an array of mixed types of objects, some of which implement an interface (iExample) which has one method - doStuff()...you can test for it like so:
code:
for(var i=0;i<myArr.length;i++){
if(myArr[i] instanceof iExample){
myArr[i].doStuff();
}
else{
trace(i +" does not implement the interface");
}
}
HTH,
K.
-
Senior Member
Thanks deadbeat, I had no idea 'instanceof' could also be used to get an object's Interface.
-
Senior Member
this seems to work when I export the movie as flash 7, it does not work when I export it as flash 6 ('instanceof' does seem to point to the object's class but not to the interface).
any idea how to implement this so it would work with Flash 6?
-
Hmmm...don't think you'll be able to do that in F6...
Alternately, you could just call the method on every object in the array - if the object doesn't have the method defined, it will simply be ignored...
K.
-
Senior Member
thanks, I did something like this:
Code:
a = myObject.interfaceMethod()
if(a!=null){
//do something with a
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|