Creating Components
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 [Link]
• Declare properties and methods for a component
• Create a reference of the Component from the User Interface
• Identify the role of assemblies in [Link]
©NIIT Creating and Managing Components Lesson 1A / Slide 1 of 24
Creating Components
Getting Started with .NET
Components
• A component is a binary executable built from a .NET project.
• The component is referenced by applications seeking the services provided by
the component.
©NIIT Creating and Managing Components Lesson 1A / Slide 2 of 24
Creating Components
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.
©NIIT Creating and Managing Components Lesson 1A / Slide 3 of 24
Creating Components
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.
©NIIT Creating and Managing Components Lesson 1A / Slide 4 of 24
Creating Components
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.
©NIIT Creating and Managing Components Lesson 1A / Slide 5 of 24
Creating Components
Life Cycle of a Component
• Components are initialized by constructors.
• Initialization can be of two types:
• Type initialization
• Type initialization is achieved by using a shared constructor.
• Shared constructor is called only once in the lifetime of a Visual Basic
.NET application.
• Instance initialization.
• Instance initialization can be achieved by using constructors, which
are not shared.
©NIIT Creating and Managing Components Lesson 1A / Slide 6 of 24
Creating Components
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.
©NIIT Creating and Managing Components Lesson 1A / Slide 7 of 24
Creating Components
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
©NIIT Creating and Managing Components Lesson 1A / Slide 8 of 24
Creating Components
Using .NET COM Components in
[Link]
• Component Object Model (COM) was used in earlier versions of Visual Basic to
create components.
• Implementation of COM is called ActiveX.
• The .NET supports COM and ActiveX objects for backward compatibility.
• A COM component from .NET can be called in the following ways:
• By converting the COM type library to a .NET assembly.
• Using COM components directly.
©NIIT Creating and Managing Components Lesson 1A / Slide 9 of 24
Creating Components
Converting COM Type Library to .NET
Assembly
• The tlbimp tool can be used to convert a COM type library to a .NET assembly.
• Following options can be used with tlbimp tool:
• out:FileName – specifies the name of the assembly to be produced.
• Namespace:NameSpace – Specifies the namespace of the assembly to be
produced.
• AsmVersion:Version – Specifies version number of the assembly to be
produced.
• reference:FileName – Specifies the file name to be used to resolve
references.
©NIIT Creating and Managing Components Lesson 1A / Slide 10 of 24
Creating Components
Using COM Components Directly
• The Com components can be added directly through Solution Explorer in .NET.
• Internally, an Runtime Callable Wrapper (RCW) is used to access the COM
components.
©NIIT Creating and Managing Components Lesson 1A / Slide 11 of 24
Creating Components
Using COM+ with [Link]
• COM+ is the basic COM and a set of additional services.
• COM+ programming is based on the following assumptions:
• In COM+, clients program on the basis of Interfaces, not Classes.
• Code is not linked statically.
• Using Com+ components, you can focus on developing business logic.
• A .NET component that takes advantage of COM+ services needs to be derived
from the .NET base class ServicedComponent.
©NIIT Creating and Managing Components Lesson 1A / Slide 12 of 24
Creating Components
Properties and Methods for a
Component
• To create a component in .NET, the user needs to identify:
• The type of component.
• Properties for the component.
• Methods for the component.
©NIIT Creating and Managing Components Lesson 1A / Slide 13 of 24
Creating Components
Using Constructors
• A component can have the following types of constructors:
• Public constructors.
• Friend constructors.
• Private constructors.
©NIIT Creating and Managing Components Lesson 1A / Slide 14 of 24
Creating Components
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.
©NIIT Creating and Managing Components Lesson 1A / Slide 15 of 24
Creating Components
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
©NIIT Creating and Managing Components Lesson 1A / Slide 16 of 24
Creating Components
Referencing the Component from the
User Interface
• A component can be tested only by referencing it from another application.
• A component can be referenced by using the Solution Explorer.
©NIIT Creating and Managing Components Lesson 1A / Slide 17 of 24
Creating Components
Role of Assemblies in [Link]
• Concept of assemblies has been introduced in the .NET to counter the problems
faced in DLL handling in previous versions.
• An assembly usually consists of:
• Manifest
• This contains the information like name and version of the assembly.
• Information about the files that the assembly uses, is also present in
it.
• Portable executable (PE)
• This contains metadata, Intermediate Language (IL) and the type
information.
©NIIT Creating and Managing Components Lesson 1A / Slide 18 of 24
Creating Components
Demo
Using .NET Components
©NIIT Creating and Managing Components Lesson 1A / Slide 19 of 24
Creating 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
[Link] application named as ConsumerApplication which will display the
information.
©NIIT Creating and Managing Components Lesson 1A / Slide 20 of 24
Creating Components
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.
©NIIT Creating and Managing Components Lesson 1A / Slide 21 of 24
Creating Components
Summary
In this lesson, you learned that:
• A component is a reusable piece of code in binary form that can be used
along with the components from other vendors, with relatively little effort.
• Types of Components:
• In-built components.
• Component classes.
• User controls.
• A component goes through different stages once you deploy it. The
constructors initialize components.
• Initialization can be of two types:
• Type initialization.
• Instance initialization.
©NIIT Creating and Managing Components Lesson 1A / Slide 22 of 24
Creating Components
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).
©NIIT Creating and Managing Components Lesson 1A / Slide 23 of 24
Creating Components
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.
©NIIT Creating and Managing Components Lesson 1A / Slide 24 of 24