Solved

Using the same function with different extended object types

Posted on 2011-03-07
2
205 Views
Last Modified: 2012-05-11
Hi still being very green with Actionscript, I wonder if you could help with the following,

For the sake of an example think of a page in a book where,

I have 4 seperate classes,  ParagraphData, PartHeadingData, FooterData and ListData each which extend from the PageItemData class, a pageItem being its namesake on a page and each class has a different set of extended properties

Each "page of the book" as such is presented in an array called pageData and contains for example 3 instances of ParagraphData, 1 PartHeadingData , 1 list etc
 
What I need to do is iterate over the pageData array and act accordingly depending on what object type is currently being examined.

I have tried the following but with no success.

for each (var pageItem:PageItemData in pageData){
                        
      //check for prefix span and add if true            
                        if (pageItem.has_Prefix_Span && pageItem.prefix_Span_Text!==""){
                              createParagraphPrefixSpan(pageItem,pElem)
                        }
                              
      //check for inline graphic      
                        if ((pageItem.paragraph_Contains_Figure)as ParagraphData){
                        pElem=figure.checkForFigure(pageItem,pElem,figureData);
                        }

The first portion works because of the properties within the PageItemData class which are common to all , but the second does not ,even if I try to cast to the correct datatype, the compiler keeps complaining about


1118: Implicit coercion of a value with static type vo:PageItemData to a possibly unrelated type vo:ParagraphData

1119: Access of possibly undefined property paragraph_Contains_Figure through a reference with static type vo:PageItemData

I know I could replace the first line with

for each (var pageItem:* in pageData){

but this seems odd to me seing as all the items being iterated over are extended from the type shown.

What am I missing here and thanks in advance

John


}
0
Comment
Question by:BloodGrinder
2 Comments
 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 500 total points
ID: 35056986
Well I'd to some checks the following way:
if(pageItem is ParagraphData) {
   var paragraphData:ParagraphData = ParagraphData(pageItem);

   ... work with paragraphData ...

} 

Open in new window


In the if-part you check if the type is the one you expect.
If it is I tend to define a local varialbe containing a casted reference to the type I did my check for. This makes working with it a lot easier.

By the way ... I wouldn't write "!==" ... I think you are looking for "!=" (Actually I don't know what !== actually does think it's a check for instance equality)
0
 

Author Closing Comment

by:BloodGrinder
ID: 35113072
Apologies I thought i had closed this 3 days ago
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Video sharing with user login 3 185
free flash maker 5 196
Begin mp4 video on first frame 6 162
Advertising design for my own Purpose 5 64
This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now