View Full Version : Using Component Class in project

Hi all,

I am new to .net development and I have this question when I am trying to doing my project and learning/reading sample code at the same time. I saw a project using Component Class and reference it through out. My question is why a Component Class is used and not just an object/class, and would you please tell me that it must be used or just an alternative?

Were they using the word 'component' in a general way, or were they talking about the class System.ComponentModel.Component (or classes deriving from it)?

I could answer better if I saw the same samples and understood the context, but here's all I can offer so far:

The classes that use Component as a base class show up in the ToolBox and can be dragged from there onto a Form. So certain items in the ToolBox that are not visible Controls, (Timer, BackgroundWorker, ImageList, ErrorProvider) all inherit from Component. It also enabled object-sharing between applications, but I haven't messed with that.

Thank you for your reply.

They are building a DataLayer project as a DLL. In there they have other classes such as Customer.vb, Performance.vb, Order.vb,...Then they have Theater.vb (together with Theater.Designer.vb, Theater.resx) which is a Component Class. If you click on a project to add new item you will see a Component Class together with other Class, Interface, Text File...That is the component I am talking about. Then when in other class definition they just use the component Theater by referencing the attributes.
For example in Performances Constructor they have:

Friend Sub New(ByVal startsAt As DateTime, ByVal duration As Integer)
row = Theater.Performances.NewRow()
row("StartsAt") = startsAt
row("Duration") = duration
End Sub

From the example you've given, I don't see any special purpose to them having Theater inherit from Component. However, they probably do have a good reason for it, but it would take reading the source code of the Theater class to understand what that is.

There are specific reasons when you would need to inherit from Component. The rest of the time, you won't.

As with any OO design, what interfaces your class implements or base classes your class inherits will be based on what your class's purpose is, and whether those interfaces/base classes are a necessary part of that. You wouldn't implement IDisposable if you don't have anything to dispose, and you wouldn't inherit from CollectionBase if your class was not going to be a Collection..