I am creating a method that accepts a IOBJECT parameter. there is multiple class that implement this interface. I need to figure out which type IOBJECT is. how would i go about doing that
+1
A:
It's not ideal, but you can use the "is" operator. Throw it into a switch of if else statment to figure things out.
if(obj is ClassA) {
//sweetness
} else if (obj is ClassB) {
//awesomeness
}
typeof will not work, as suggested in the other reply. It will likely return "object" in all cases. instanceof will work though.
Tyler Egeto
2009-12-30 19:06:55
So, how would i implement the"is" or the instanceof in a Case Statement ?
numerical25
2009-12-30 20:14:05
Ya don't use a case, use if else's. You could use switch if you were working with getQualifiedClassName(), but just use if else, its simpler.
Tyler Egeto
2009-12-31 19:04:21
A:
You can do getQualifiedClassName() to get the class name of the object. You can also use describeType() which gives you a more complete description of all the methods and properties of the object.
There is information about both here: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html
It doesn't sound like an ideal situation though. You may want to do something where you can standardize the way you handle all items. For example:
public interface IObject {
function doSomething():void;
}
Then...
function myMethod(obj:IObject):void {
obj.doSomething();
}
Mims H. Wright
2009-12-30 20:26:30
That sounds like a good idea. BUt I want to do something with obj, store it in particular varibles depending on the type. So I need to use the case statement to find out what type it is. just trying to figure out how to apply the is or instanceof with the case statement.
numerical25
2009-12-30 20:41:19
it appears that I cant call a method from within the Interface data type, I have to cast it back to its orginal type in order for it to work. in order words. iobj.doSomething(); doesnt work. but Object(iobj).doSomething(); does. am i missing something ?
numerical25
2009-12-30 22:08:00