471,337 Members | 1,061 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Class structure questio n ???

Dear all

I have an Object that I will call for instance VEHICULE. This object
contains some other type of objects like CAR and BICYCLE

VEHICULE has necessary at least one object of type CAR or BYCICLE

My question is that if I am at the VEHICLE lass level, it can contains a
certain amount of CAR class and BICYCLE class.

What is the best way to handle the content of VEHICLE, is it good to handle
a List of object of other type like as follow:

public class VEHICULE

dim ListOFCar as system.collection.Arrays
dim ListOFBycicle as system.collection.Arrays

class CAR
.....
end class

class BYCICLE
...
end class

end class

is it correct or there is a better way like Inheritence ?

thnaks for your help
regards
serge
Jul 21 '05 #1
4 1254
Serge,

A vehicle has mostly wheels (A car mostly four a Bicycle mostly two), so for
the wheels you can take an collection of wheels.

However a Car and a Bicycle would in my way inherid from verhickle.

For steering has a Car a simple wheel so that needs probably not to be
overriden.

A Bicycle has a handle-bar that contains a lot of properties itself, so that
should override the steering from the verhicle.

However a bicycle has no engine, so that can be shadowed (your showed code
is VBnet), with by instance setting it consequently to nothing or by
throwing an exception).

Just my idea,

Cor
Jul 21 '05 #2
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
Jul 21 '05 #3
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

Jul 21 '05 #4
Serge,

In my opinion is in this article everything you ask for.

http://msdn.microsoft.com/library/de...assinherit.asp

I hope this helps,

Cor
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.