[Flashcoders] AS 2.0 and Casting to datatypes

2006-05-31 Thread Stephen Ford
Thanks Ian and Mike - much appreciated help on the concept of casting.
Casting makes sense now.
 
Stephen.___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] AS 2.0 and Casting to datatypes ...

2006-05-31 Thread Ian Thomas

Whoops - instead of this:

var animal2:Animal=z.removeAnimal("fido");
if (animal2 instanceof Dog)
{
  var myDog:Dog=Dog(animal2);
  animal2.fetchNewspaper();
}


I meant:

var animal2:Animal=z.removeAnimal("fido");
if (animal2 instanceof Dog)
{
  var myDog:Dog=Dog(animal2);
  myDog.fetchNewspaper();
}


Silly me - sorry!

Ian
___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] AS 2.0 and Casting to datatypes ...

2006-05-31 Thread Ian Thomas

Hi,
 As Mike says, casting is used to tell the compiler that you're sure
about the type of an object - where the compiler would otherwise
report an error, because it's unsure.

 This is commonly needed in situations such as when you're using an
object designed for handling generic types to manage specific types.

For example, say you have a Zoo object, for managing animals:

class Zoo
{
 public function addAnimal(id:String,animal:Animal):Void;
 public function removeAnimal(id:String):Animal;
}

class Animal
{
 public function getNoise():String;
}

var z:Zoo=new Zoo();
var animalA:Animal=new Animal();
z.addAnimal("someAnimal",animalA);
var animalB:Animal=z.removeAnimal("someAnimal");
trace(animalB.getNoise());

And then you write an application in which you are simply dealing with
objects of type Dog, which are a subclass of Animal. Instead of
writing a new Kennel object, you just decide to use Zoo - after all,
Dogs are Animals, right?
class Dog extends Animal
{
  public function fetchNewspaper():Void;
}

The following code works:
var z:Zoo=new Zoo();
var dog:Dog=new Dog();
z.addAnimal("fido",dog);

But this breaks:
var dog2:Dog=z.removeAnimal("fido");

because the compiler only knows that removeAnimal returns an Animal -
it can't guarantee that it will be a Dog. Because you are sure what
you are doing - you are sure, right? - you can write the following to
tell the compiler it's all okay...
var dog2:Dog=Dog(z.removeAnimal("fido"));
And now you can call dog2.fetchNewspaper() and all will be fine.

If you're _not_ sure of the type of Animal retrieved - i.e. you know
it's an Animal, but you don't know for sure if fido is a Dog, you can
do the following:
var animal2:Animal=z.removeAnimal("fido");
if (animal2 instanceof Dog)
{
 var myDog:Dog=Dog(animal2);
 animal2.fetchNewspaper();
}

And now fetchNewspaper() will only ever get called if animal2 is actually a dog.

I hope that makes some sense. :-)

Cheers,
 Ian


>
> Can anyone explain what Casting is.
>
> For example, in Moock's Essential AS 2.0, chapter 18 MVC clock example,
> page 407:
>

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] AS 2.0 and Casting to datatypes ...

2006-05-31 Thread mike cann

Hi,

Casting is telling the program expeicitly what a variable it so that the
compiler doesn't throw a wobbler when you try to perform an operation on
said variable.

In your example:

var info:ClockUpdate = ClockUpdate(infoObj);

infoObj is probably defined as type Object? And even though you know that it
is of type ClockUpdate the compiler doesn't know, so when you try to do:

info = infoObj

the compiler will think you are trying to do this:

variable:ClockUpdate = variable:Object

and wont know what to do.



I hope this made some sense, I'm sure others will be able to explain it
better then I.

Mike




On 31/05/06, Stephen Ford <[EMAIL PROTECTED]> wrote:


Myello,

Can anyone explain what Casting is.

For example, in Moock's Essential AS 2.0, chapter 18 MVC clock example,
page 407:

// Cast the generic infoObj to the ClockUpdate datatype.varinfo:ClockUpdate = 
ClockUpdate(infoObj);

The infoObj at this point is already a ClockUpdate datatype, so why is it
being Cast ? and what is Casting in general.

Thanks,
Stephen.___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


[Flashcoders] AS 2.0 and Casting to datatypes ...

2006-05-30 Thread Stephen Ford
Myello,
 
Can anyone explain what Casting is.
 
For example, in Moock's Essential AS 2.0, chapter 18 MVC clock example, page 
407:
 
// Cast the generic infoObj to the ClockUpdate datatype.var info:ClockUpdate = 
ClockUpdate(infoObj);
 
The infoObj at this point is already a ClockUpdate datatype, so why is it being 
Cast ? and what is Casting in general.
 
Thanks,
Stephen.___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com