Professional Documents
Culture Documents
Objectives
In this lesson, students will learn to:
• Identify the types of components
• Identify the various stages in the life cycle of a component
• Implement polymorphism in components
• Use .NET COM components in VB.NET
• Declare properties and methods for a component
• Create a reference of the Component from the User Interface
• Identify the role of assemblies in VB.NET
Introducing Components
• You can create the component once and reuse it whenever necessary.
• You can also develop components as a third party provider.
• When you use the components, you are not inheriting a class but using a
binary code in your applications.
Types of Components
• Components can be of three types:
• In-built Components
• The .NET class framework comes with in-built components, such as
Timer and ServiceController.
• You can use the components that come with the .NET class
framework in your applications.
• Component Classes
• A class becomes a component when it follows defined standards of
interaction.
• All components derive from the Component class.
• User Controls
• User Controls can be defined as bundles of standard controls.
Characteristics of a Component
• The name of a component class should be short and meaningful. It should be a
combination of whole words with a capitalized initial character for each word.
• You can control the use of a component by using proper access level for the
constructors.
• You can implement the IComponent interface to create a component.
• You should group related components together in separate namespaces.
Polymorphism in Components
• The ability of a class to implement functions with the same name that are
performing different actions depending on how they are called is known as
polymorphism.
• Polymorphism can be implemented by:
• Implementing an interface.
• Inheritance
• Using abstract classes.
Implementing Interfaces
• An interface defines the behavior of a class.
• An interface can declare properties, methods, and events.
• An interface can be implemented by more than one class.
• An interface can be declared as :
Interface interfacename
Sub functionname()
End Interface
Using Constructors
• A component can have the following types of constructors:
• Public constructors.
• Friend constructors.
• Private constructors.
Adding properties
• Declaring a property allows you to control how a value is changed or read.
• Calculated values can be shown as properties even when they are not stored
as actual data in the class.
• Property statement can also incorporate error handling.
Adding methods
• If a method is declared Private, only methods within the same class can call
that method.
• A method in a class is a procedure that performs some sort of operation on the
data within the class and may or may not return.
• A method is declared as:
Public Function functionname() As Integer
Demo
Using .NET Components
Problem Statement
• Build a .NET DLL component ‘PickServerTime’ which can get the current date
and time from the host computer and then use the component in another
VB.NET application named as ConsumerApplication which will display the
information.
Solution
• To build and use the ‘PickServerTime’ component in an application named as
‘ConsumerApplication’ , you need to perform the following steps:
1. Create a project.
2. Add properties and functions to the component.
3. Build the component.
4. Create a user interface to test the component.
Summary
In this lesson, you learned that:
Summary (Contd.)
• In .NET programming environment, COM components are called:
• By converting the COM type library to a .NET assembly.
• By using COM components directly.
• The tlbimp tool uses the metadata of a COM type library to do the
conversion.
• To create a component in .NET, the user firstly needs to identify the type of
component and also its properties and methods.
• In .NET programming environment, a component can have the following
types of constructors:
• Public constructor.
• Friend constructor.
• Private constructor
• An assembly contains information about the files on which the component
depends and the location of these files.
• An assembly consists of a manifest and the portable executables (PE).
Summary (Contd.)
• A manifest consists of information such as the name and version of the
assembly.
• A portable executable consists of the IL code, type information, and
metadata.