473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1355
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Victor Hannak | last post by:
I have two classes derived from a base class. The two derived classes each utilize a structure that is slightly different from one another. i.e. DerivedClass1: struct NodeStruct { float...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
3
by: David Lozzi | last post by:
Howdy, I've discovered how to create and use a class in ASP.NET. However, when is the best time to use a class? For example, I am currently using session variables to store user information (user...
4
by: serge calderara | last post by:
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.