PDA

View Full Version : bubbling down



ofeet
February 26th, 2009, 02:36 PM
is there such a thing? it's either this or "for each"

pretend I have 6 red cars and 14 blue cars in my ParkingLot class. I want ParkingLot to tell just the red cars to drive.

In my mind it works as follows:

for each(var car in cars_Array) {
if(car is RedCar){
car.drive();
}
}
thoughts?

Krilnon
February 26th, 2009, 02:44 PM
That code looks like it would work.

ofeet
February 26th, 2009, 03:35 PM
I think it's my only option :(

was kind of hoping there's a "cooler" way

MurtenSaerbi
February 26th, 2009, 03:44 PM
What kind of cooler way do you want?

You could check the type of class it is but I presume you have a class called Car and that the carcolor is a property.

You could also check out the property directly if it's public:



if(car.carcolor == "red") {
car.drive();
}


What exactly were you hoping for?