Class Vs Component Class

  • Thread starter Thread starter Rajesh Abraham
  • Start date Start date
R

Rajesh Abraham

I am trying to understand the difference between Class &
Component Class.

What are the advantages of using one over the other?

When should I prefer to use one over the other?

Any good documentation on this subject. Also any sample
projects showing the same functionality implimented using
both the methods so as to make the difference clear would
be appretiated.

Thanks,

Rajesh Abraham Chacko
 
The simple answer is that a component derives from Component (or more
accurately, implements IComponent). IComponent means two things: firstly the
component is expected to have resources that require disposing (IComponent
derives from IDisposable), secondly, it is expected to be used with a
component container (which is accessible through the Site property).
Typically when you component is used in your application you do not use a
component container. The Site property is mainly used when you use a
designer with the component. So, class or component? Create a component when
you want to use it with the designer (ie drag and drop from toolbox to a
form or a component in the designer). If you don't want to use it with a
designer then it can be either a component of a class.

When you derive from Component you get some more features, in particular
GetService() and Events. The Events property provides a more efficient way
of storing events than the compiler will generate when you use the event
keyword. (The Events property uses a EventHandlerList, which means that your
class has a single data member, this class grabs more memory only when new
delegates are added to the property. Compare this to using the event
keyword: there is a data member for every event that your class supports, so
for a Control this would be about 60.) GetService()allows you to provide
additional services on your component, typically the service is actually
provided by a separate object. Again, if you have a class that may raise
many events then deriving from Component will save you memory (but you have
to implement the event methods to use the Events property).

Richard
 
I am trying to understand the difference between Class &
Component Class.
What are the advantages of using one over the other?

Everything is a class - every component is a class, too.

A class is the most basic part you can have - a component adds some
functionality, e.g. you put it on the Toolbox and grab it from there
and drop it on your form - a "generic" class cannot be used that way.

A component which has been dropped on a form can be manipulated by
means of the Property Inspector in our IDE - you can set and change
properties (that you expose) at design-time, in your IDE (and in code,
too, of course). You can't do that with a generic class (since you
can't drop it onto a form, so it will never show up in your visual IDE
design tools).

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
GetService() and Events in Component

Hi,

This was really a good article. Can you please give more information about GetService() and Events in Component that u have specified?

Thanks
 
Back
Top