IMHO, it makes no difference where the object is defined. In general, if
the object returned by a function call or accessor is not the type you
think it is, then you must cast it to the correct type before calling
methods or accessors defined in that type.

It gets a little bit complicated, but the crux of the problem is that
every object has two types: its static (compile-time) and its dynamic
(runtime) type. For instance I can say:

var o : Object = new String( "Hello World" );

The static type of o is Object, but its actual runtime type is String. I
can do this because String derives from Object. It's always possible to
assign an object to an instance of a class from which that object
derives.

Casting tells the compiler that you want to ignore the static type of an
object, and treat it as its dynamic type.

For example:

var myArray : Array = [ 1, 2.0, "Hello World" ];
var s : String = String( myArray[ 2 ] );
var l : int = String( myArray[ 2 ] ).length;
var o : Object = myArray[ 1 ];
var n : Number = Number( o );

Arrays can hold any object. Because of this, when you get an element out
of an Array, it is returned to you as an Object regardless of its actual
type. Casting tells the compiler that you, the programmer, know that the
returned Object is actually some other type. In the example above, I can
successfully cast Objects to String and Number.

The compiler will generate an error if the class of the object being
cast is not a super-class of the class your casting it to. Since String
and Number both derive from Object, the examples above work. But this
would generate a compiler error:

var b : VBox = new VBox();
var s : String = String( b );

because String does not derive from VBox.

Casting can generate runtime errors as well. The compiler can only check
static type information. If the actual type of the object being cast
does not derive from the type it's being cast to, you will get a
run-time error. For example:

Var s : String = String( myArray[ 1 ] );

Because the element at position 1 in the array is a Number, and because
Number does not derive from String, this command will generate a
run-time error. (This might actually not generate an error -- I haven't
tried it --  but only because there's some voodoo going on behind the
scenes that coerces between unrelated types.)

This is a very round-about way of saying that I think the Cairngorm
example is wrong. There is no need to cast model.loginVO to LoginVO
before accessing the username property. In the ViewLocator class, the
loginVO property is typed as LoginVO, so no casting is necessary. It's
runtime type matches its static type.

Tobias.

-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Suresh Akula
Sent: Tuesday, April 25, 2006 2:05 PM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Flex 2 B 2 + Cairngorm 2 - Simple Question

LoginVO is user defined object and where as Date is
System defined object, no need to type the these
objects.

--Suresh



--- Darren Houle <[EMAIL PROTECTED]> wrote:

> But when?  Why?  In every case?  If that was a
> generic, universal rule and
> you're supposed to cast everything as it's original
> object type then a
> little further in the line we should see:
>
> ' at ' + Date(model.loginDate) }" />
>
> I'm just curious why the LoginVO is cast here.  Is
> there some rule like "you
> don't have to cast an object when simply accessing
> an attribute of that
> object, but you do have to cast an object if it
> contains other objects with
> an attributes you need to access."  That sounds
> kinda silly to me, that's
> why I'm asking :-)
>
> Thanks!
> Darren
>
>
>
> >From: Suresh Akula <[EMAIL PROTECTED]>
> >Reply-To: flexcoders@yahoogroups.com
> >To: flexcoders@yahoogroups.com
> >Subject: Re: [flexcoders] Flex 2 B 2 + Cairngorm 2
> - Simple Question
> >Date: Tue, 25 Apr 2006 12:59:59 -0700 (PDT)
> >
> >Hello Darren,
> >
> >    its good partice to typecast with orginal
> object
> >and invoke the object members.
> >
> >--Suresh Akula.
> >
> >
> >--- Darren Houle <[EMAIL PROTECTED]> wrote:
> >
> > > I'm fairly new to Flex and really new to
> Cairngorm
> > > and am trying to break
> > > Cg2 down and understand it (I'm not trying to
> > > understand Cg.99 and then
> > > learn Cg2, I'm just jumping into 2.)  I have a
> quick
> > > question:
> > >
> > > In the Cg2 sample CairngormLogin.mxml theres a
> Label
> > > in the "loggedIn"
> > > VBox...
> > >
> > > <mx:Label text="{ 'Logged in as ' + LoginVO(
> > > model.loginVO ).username + ' at
> > > ' + model.loginDate }" />
> > >
> > > This seems to still work fine when changed to...
> > >
> > > <mx:Label text="{ 'Logged in as ' +
> > > model.loginVO.username + ' at ' +
> > > model.loginDate }" />
> > >
> > > Is there some reason why the first form must be
> used
> > > over of the second
> > > form?  If I were writing this code myself from
> > > scratch it would seem more
> > > intuitive to bind directly to the
> > > model.loginVO.username.  Am I missing
> > > something?
> > >
> > > Thanks,
> > > Darren
> > >
> > >
> > >
> >
> >
> >__________________________________________________
> >Do You Yahoo!?
> >Tired of spam?  Yahoo! Mail has the best spam
> protection around
> >http://mail.yahoo.com
> >
> >
> >--
> >Flexcoders Mailing List
> >FAQ:
>
http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> >Search Archives:
>
http://www.mail-archive.com/flexcoders%40yahoogroups.com
> >Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
>
>
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.com
Yahoo! Groups Links










--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




SPONSORED LINKS
Web site design development Computer software development Software design and development
Macromedia flex Software development best practice


YAHOO! GROUPS LINKS




Reply via email to