A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: check if objects's X Class implements interface Y

  1. #1
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315

    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

  2. #2
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    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.

  3. #3
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315
    Thanks deadbeat, I had no idea 'instanceof' could also be used to get an object's Interface.

  4. #4
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315
    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?

  5. #5
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    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.

  6. #6
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center