HI,
thnaks for zour reply, it helps partly
In fact I was wondering if the classes Bicycle and car should be defined
as nested class of vehicule or outside.
If they are netsed class from vehicule is there a way that class vehicule
access data of it nested class? same thing in reverse is they a way that
nested class can access to meber of parent class ?
is also a way to doit without using interface or inheritance, or is it
something impossible. In fact I try to see what would be the different if I
use nested class
regards
serge
"baylor" wrote:
Serge, i'm not sure i understand your question so let me tell you what i
think you're saying. You have several different types of vehicles such as
cars and bicycles. You also have a collection that will contain a mixture of
different types of vehicles
If that is true, your collection should hold objects of one type. Any object
in that collection would want to either inherit from that type or implement
an interface of that type. An example:
public class Vehicle
{
protected string name;
}
public class Car : Vehicle
{
public Car(string name) { this.name = name; }
}
public class Bicycle : Vehicle
{
public Bicycle(string name) { this.name = name; }
}
public class Vehicles
{
ArrayList collection = new ArrayList();
public void add(Vehicle aVehicle) { collection.Add(aVehicle); }
}
Your Application
Vehicles myGarage = new Vehicles();
myGarage.add(new Car("Ferrari"));
myGarage.add(new Bicycle("Schwinn"));
When you get the values back, they'll be of type Vehicle, not Car or
Bicycle. If you want to turn them back into that, you can recast it
Vehicle myVehicle = (Vehicle) collection[1];
if (myVehicle is Car)
{
Car myCar = (Car) collection[1];
}
else if (myVehicle is Bicycle)
{
Bicycle myBike = (Bicycle) collection[1];
}
Hope that's what you were looking for
-baylor