Interesting. Thanks for the clarification. I guess I have been thinking that if 
FlexBuilder doesn't show something as an option in the code completion menu 
that I should avoid using it, or should only use it with bracket syntax.

This might just be me, but I would go a little further and say that I like 
using brackets to call out that I am using a dynamic property. On the other 
hand, if I am using a strongly typed object, then I like to cast, and use the 
dot notation. And if I cast first, then the code completion will in fact show 
the property as an option.

So I would want to do something like this:

var vo:VoClass = new VoClass; // some custom class
var obj:Object = {strEMail: "foo"};
var acUser:ArrayCollection = new ArrayCollection();
acUser.addItem(obj);
acUser.addItem(vo);
var strEMail:String = acUser.getItemAt(0)["strEMail"] as String;
strEMail = (acUser.getItemAt(1) as OSData).comment;

Admittedly, as you have made clear, this approach is not necessary. However, I 
do think it has some benefit, particularly since it allows one to both lean a 
little more on FlexBuilder, and give the compiler a little more information to 
work with.

However, having said all that, you are nonetheless quite right that I misspoke 
about what can be done.




----- Original Message ----
From: Gordon Smith <[EMAIL PROTECTED]>
To: [email protected]
Sent: Friday, July 18, 2008 2:26:47 PM
Subject: RE: {Disarmed} Re: [flexcoders] Variable Typecasting


There is no reason why 
 
    strEMail = acUser.getItemAt( 0).strEMail;
 
shouldn't both compile and run if the 0th
item in the ArrayCollection acUser has a property named strEMail of type
String. And, in fact, I had no problem compiling and running
 
    var acUser:ArrayCollect ion
= new ArrayCollection( [ { strEMail: "foo" } ]);
    var strEMail:String =
acUser.getItemAt( 0).strEMail;
    trace(strEMail) ;
 
It traced "foo" as one would
expect.
 
Since getItemAt()' s return type is Object,
the compiler lets you access any property on it. (Of course, strong typing is
better practice). And when you access a property on an Object, the compiler
treats that property as having type *, which can be assigned to a String var
without casting it to a String.
 
Gordon Smith
Adobe Flex SDK Team
 

________________________________
 
From:[EMAIL PROTECTED] ups.com [mailto: [EMAIL PROTECTED] ups.com ] On Behalf 
Of Sid Maskit
Sent: Friday, July 18, 2008 1:35
PM
To: [EMAIL PROTECTED] ups.com
Subject: Re: {Disarmed} Re:
[flexcoders] Variable Typecasting
 
As someone else explained, it is better to use strongly typed objects
then generic ones. However, if you are going to use a generic one, something
like the following should work:

strEMail =
GlobalVars.instance .acUser.getItemA t(0)["strEMail" ] as
String;

Two things to note here. First, if you are using a generic object, you can
access its properties using bracket offsets with the property name in quotes.
You need to do this since what you actually have is an associative array, and
the compiler cannot know what its keys are. Second, the preferred method of
casting is not to use a constructor, a la String(whatIAmCasti ng), but to
use the as operator, a la whatIAmCasting as String.
 
----- Original Message
----
From: Scott <[EMAIL PROTECTED] us>
To: [EMAIL PROTECTED] ups.com
Sent: Friday, July 18, 2008 6:48:58 AM
Subject: RE: {Disarmed} Re: [flexcoders] Variable Typecasting
Wow.  This was incredibly frustrating but after playing around
(and rebooting my computer numerous times from the debugger crashing) I finally
figured out how to reference the data in the arraycollection.
 
I didn’t have to typecast it.  However, I had to process
it one function at a time.
 
I tried strEMail = String(GlobalVars. instance. acUser.getItemAt
(0).strEMail) ; and that didn’t work; it kept throwing an error that it
didn’t know about strEMail.  After playing around I discovered that
if I broke out the functions it would work.
 
This is what worked for me:
 
private var objUser:Object;
 
private function blah( acUser:arrayCollect ion): void
{
 
           
objUser = GlobalVars.instance .acUser.getItemA t(0);
           
strEMail = String(objUser. strEMail) ;
}
 
After I got the results I wanted, I tried it again as one line and
Flex threw an error that it again didn’t know about strEMail. 
Perhaps that’s a bug…?
 
-sj

________________________________
 
From:[EMAIL PROTECTED] ups.com [mailto:flexcoders@ yahoogroups. com] On Behalf 
Of Scott
Sent: Friday, July 18, 2008 8:12
AM
To: [EMAIL PROTECTED] ups.com
Subject: {Disarmed} Re:
[flexcoders] Variable Typecasting
 
It seemed to have taken that but now it’s still not pulling
the data in as I would expect it.
 
The getItemAt(x) in reading looks like it would pull in the whole
record at ‘x’.  However, it won’t let me refer to the
record as getItemAt(0) .strEMail since it doesn’t know strEMail exists in
that collection.
 
In structures (in C++/Coldfusion/ etc…) I had to define the
variable names within the structure.  However, in AS3 I just defined the
variable: 
 
[Bindable] public var acUser:ArrayCollect ion = new
ArrayCollection( );
 
How do I access the strEMail from the array collection (or at least
tell the Flex compiler that the variable really does exist).
 
Thanks 
 sj
 

________________________________
 
From:[EMAIL PROTECTED] ups.com [mailto:flexcoders@
yahoogroups. com] On Behalf Of Peter
Witham
Sent: Friday, July 18, 2008 12:02
AM
To: [EMAIL PROTECTED] ups.com
Subject: {Disarmed} Re:
[flexcoders] Variable Typecasting
 
Have you
tried 

 
strEMail = String(GlobalVars. instance. acUser.getItemAt
(0));

 
Do not have my
machine in front of me to test but pretty sure you can cast something that way.
I know that's the way I do it to cast MovieClip names.

 
Regards,
Peter
On 7/17/08, Scott <[EMAIL PROTECTED]
us> wrote:
I'm
trying to pull a variable from an arrayCollection which I know is type String.
 
Here's
the code:
 
      publicvarstrEMail:String;
           
…
Private
function … 
{
           
strEMail = GlobalVars.instance .acUser.getItemA t(0);
}
 
That
above line of course throws an error because it's not sure that the variable at
(0) is actually a string even though I know it is.  In C++ I would just
tell it using a typecast that it is a string.
 
How
can I copy that element into a variable?
 
Thanks!



-- 
Peter Witham
http://www.evolutio
ndata.com
Internet and Multimedia developer
Certified Flash Designer. 

-- 
This message has been scanned for viruses and 
dangerous content by MailScanner, and is 
believed to be clean. 

-- 
This message has been scanned for viruses and 
dangerous content by MailScanner, and is 
believed to be clean. 
     


      

Reply via email to