VB .net tutorial - 3

48
Implementing Object-oriented Programming in Visual Basic .NET ©NII T Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 1 of 48 Objectives In this lesson, you will learn to: Identify the advantages of using classes and objects Identify the use of structures Identify the use of abstract classes in Visual Basic .NET Identify the use of interfaces Identify the differences between interfaces and abstract classes Identify the use of assemblies Identify the application hierarchy in Visual Basic .NET Create and instantiate a class

Transcript of VB .net tutorial - 3

Page 1: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 1 of 48

Objectives

In this lesson, you will learn to:

Identify the advantages of using classes and objects

Identify the use of structures

Identify the use of abstract classes in Visual Basic .NET

Identify the use of interfaces

Identify the differences between interfaces and abstract classes

Identify the use of assemblies

Identify the application hierarchy in Visual Basic .NET

Create and instantiate a class

Page 2: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 2 of 48

Objectives (Contd.)

Declare and import namespaces

Create an inherited form in Visual Basic .NET

Page 3: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 3 of 48

Object-orientation in Visual Basic .NET

Visual Basic .NET:

Supports the four pillars of object-oriented programming:

Encapsulation

Abstraction

Inheritance

Polymorphism

Uses predefined classes provided by the .NET Framework.

Allows you to create your own classes.

Supports structures that enable you to create your own data type Data Adapter.

Also has full support for interfaces.

Page 4: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 4 of 48

Classes and Objects

Class

Is a conceptual representation of all the entities that share common attributes and behaviors.

Defines the attributes and behaviors of all the instances of the class.

Object

Is an instance of a class.

Has individual copy of the common attributes and share a common set of behaviors.

Page 5: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 5 of 48

Advantages of Using Classes and Objects

Maintenance of code by introducing modularity.

Encapsulation of internal complexities in code from end-users.

Reuse of code across applications.

Support for a single interface to implement multiple methods.

Page 6: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 6 of 48

Structure

Is a generalization of a user-defined data type (UDT).

Is created when you want a single variable to hold multiple types of related data.

Is declared by using the Structure and End Structure statements.

Can also include procedures as its members.

Supports event handling.

Page 7: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 7 of 48

Structure (Contd.)

You can specify the accessibility of the data members within a structure by using one of the following access modifiers:

Public

Protected

Friend

Protected Friend

Private

Page 8: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 8 of 48

Structure (Contd.)

Storing and accessing data within/from a structure:

Is done by using the .(dot) notation.

Example

Dim ord1 As order_details

ord1.Inv_no = "I0001“

ord1.Ord_dt = #5/31/2001#

If ord1.Inv_no = "" Then

MsgBox("Please enter the Invoice number")

End If

Page 9: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 9 of 48

Classes Vs. Structure

A few similarities between a class and a structure are:

Both can have members, including constructors, properties, constants, and events.

Both can implement interfaces.

Both can have shared constructors, with or without parameters.

Page 10: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 10 of 48

Classes Vs. Structure (Contd.)

A few dissimilarities between a class and a structure are given in the following table:

Class Structure

A class is inheritable from other existing classes.

A structure is not inheritable.

A class can have instance constructors with or without parameters.

A structure can have instance constructors only if they take parameters.

A class is a reference type. A structure is a value type.

The members of a class can be initialized within the class declaration.

The members of a structure cannot be initialized within the structure declaration.

Page 11: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 11 of 48

Abstract Class

Contains the skeleton of the methods that derived

classes can implement. ExamplePublic MustInherit Class EmployeeDetails

Public MustOverride Sub Annual_Salary(ByVal m As Integer)

End Class

Public Class Emp_details

Inherits EmployeeDetails

Public Overrides Sub Annual_Salary(ByVal m As Integer)

' Write the implementation code here

End Sub End Class

Page 12: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 12 of 48

Abstract Class (Contd.)

You can create an abstract class by using the MustInherit keyword in the class definition.

You can define a method in an abstract class using the MustOverride keyword.

You must implement the method in derived classes using the Overrides keyword.

If a derived class of an abstract class does not implement one of the abstract methods of the abstract base class, it also must be declared with MustInherit keyword as it also becomes an abstract class.

Page 13: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 13 of 48

Interface

Can contain only the declaration of members such as properties, methods, and events.

Enables you to separate the definition of objects from their implementation so that the objects can evolve without the risk of introducing incompatibility in existing applications.

Is declared by using the Interface and End Interface statements.

ExampleInterface Iorderdetails

Property CustName() As String Sub UpdateCustStatus() Event Update_Complete()

End Interface

Page 14: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 14 of 48

Interface (Contd.)

Statements are Public by default.

Can inherit members from an existing interface by using the Inherits statement.

Example

Interface Validate_Cust Sub Validate_Custname()

End Interface Interface Iorderdetails Inherits Validate_Cust

Property CustName() As String Sub UpdateCustStatus() Event Update_Complete()

End Interface

Page 15: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 15 of 48

Differences Between Interfaces and Abstract Classes

Interfaces represent the highest level of abstraction in object-oriented programming because all the methods in an

interface do not have any implementation.

In contrast, the abstract classes that are created by using the MustInherit keyword might contain a method

that has a body.

Page 16: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 16 of 48

Assemblies

Consist of a single or multiple executable and resource files that contain information necessary for deploying and maintaining the versions of an application.

Enable users to use an application even when the application is not registered in the System Registry, since:

Every assembly is self-describing through metadata.

Every reference to a type is scoped by an assembly reference.

.NET can automatically locate referenced assemblies.

Page 17: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 17 of 48

Assemblies (Contd.)

The application hierarchy in Visual Basic .NET is illustrated in the following figure:

Page 18: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 18 of 48

Assemblies (Contd.)

Consist of manifest, module, and type.

Manifest is the descriptor of the assembly. It contains information on:

The name and version number of the assembly.

Its interaction with other assemblies.

The types exposed by the assembly.

Security permissions required by the assembly.

Page 19: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 19 of 48

Assemblies (Contd.)

Module is either a DLL or EXE. It contains:

Compiled code in intermediate languages.

Metadata associated with the module.

Optionally, the manifest for the assembly.

Type in Visual Basic .NET can be a class or a structure that contain data and logic affecting the data.

Page 20: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 20 of 48

Just a Minute… 1. Why is a structure called a composite data type?

2. Identify the syntactical error in the following declaration of an interface:

Interface Iproductdetails

Property ProductName() As String

Sub UpdateCustStatus()

Label1.Text=“This is a Sub Procedure to check the status of

the product”

End Sub

Event Update_complete()

End Interface

3. How are interfaces different from abstract classes?

Page 21: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 21 of 48

Problem Statement 3.D.1

The call centers at Diaz Telecommunications need to maintain customer information to respond to customer queries. The details of the customers need to be accepted through a graphical interface. The customer information also needs to be stored in the relevant memory variables. The details of the customers essentially include customer id, customer’s first name, customer’s last name, address, telephone number, and customer’s e-mail id.

Page 22: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 22 of 48

Task List

Identify the data that needs to be captured.

Identify the type of user interface to be used.

Identify the user interface components to accept data.

Identify the mechanism to store data in the relevant memory variables.

Identify the memory variables to store customer information.

Perform appropriate steps to create the user interface screen, as designed.

Add a class to the project.

Write the code to add the relevant members to a class.

Page 23: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 23 of 48

Task List (Contd.)

Write the code to instantiate the class.

Write the code to store and retrieve data from the class.

Save the application.

Run the application.

Page 24: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 24 of 48

Task 1: Identify the data that needs to be captured.

Result:

As per the problem statement, the data that needs to be captured is:

Customer ID

First Name

Last Name

Address

Telephone number

E-mail ID

Page 25: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 25 of 48

Task 2: Identify the type of user interface to be used. There are three types of user interfaces:

Windows Forms Web Forms Console

Result:

Since the requirement stated in the problem statement is for a graphical user interface, you will use Windows Forms

as the user interface.

Page 26: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 26 of 48

Task 3: Identify the user interface components to accept data.

Guidelines for designing a user interface:

Identify the name and title of the form.

Identify the controls required.

Recommend suitable prefixes that you can use for various controls:

Object Prefix Example

Form frm frmCustomerdetails

Label lbl lblCustomerName

TextBox txt txtCustomerName

Button cmd cmdSave

Page 27: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 27 of 48

Task 3: Identify the user interface components to accept data. (Contd.)

Result:

Based on the guidelines, set the form name as frmCustomerdetails and the text, which is the title of the form, as Customer Details.

To make a user-friendly interface, add label controls to display static text for the customer details: Customer ID, First Name, Last Name, Address, Telephone number, and Email ID. Next, add text box controls to accept information for each customer detail. In addition, add two buttons to save and retrieve the data stored for the customer.

Page 28: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 28 of 48

Task 4: Identify the mechanism to store data in relevant memory variables.

In Visual Basic .NET, you can store data in relevant memory variables by implementing the object‑oriented features. The object-oriented features enable you to create

a class that has member variables to store data during data processing.

Result:

Since the requirement stated in the problem statement is to store customer information in the relevant memory

variables, a class called Customer needs to be created that will have member variables to store customer information.

Page 29: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 29 of 48

Task 5: Identify the memory variables to store customer information.

Result:

Since the customer information is to be stored in the member variables of the Customer class, you need to add

variables to the Customer class. Each member variable of the Customer class should store a particular type of customer detail. You must ensure that the data type of a member variable matches the data type of the customer detail stored by the variables. Six member variables need to be declared in the Customer class.

Page 30: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 30 of 48

Task 6: Perform appropriate steps to create the user interface screen, as designed.

Task 7: Add a class to the project.

Namespace

Is a naming scheme that helps you organize the classes available in an application so that they can be easily found.

Enables you to avoid name collisions.

Is created using the Namespace keyword.

Every project in Visual Basic .NET has a root namespace, which is set in the Property page of the project.

You can use namespaces explicitly through direct addressing or implicitly through the Imports statement.

Page 31: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 31 of 48

Task 8: Write the code to add the relevant members to a class.

Task 9: Write the code to instantiate the class.

Object Lifetime

The life of an object begins when an instance of a class is created using the New keyword.

The life of an object ends after it goes out of scope or is set to Nothing and is released by the .NET Framework.

Constructors

Are of two types, shared constructors and instance constructors.

Page 32: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 32 of 48

Shared Constructors

Used to initialize the variables that are declared with the Shared keyword.

Have an implicit Public access.

Will not run more than once during a single execution of a program.

Example

Public Class class1

Shared x As Integer

Shared Sub New()

x = 10

End Sub

End Class

Page 33: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 33 of 48

Instance Constructors

Used to initialize variables that are declared with Dim, Public, Private, Friend, Protected, and Protected Friend keywords.

Allow access to shared variables.

Example

Public Class class1

Dim x As Integer

Public Sub New()

x = 10

End Sub

End Class

Page 34: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 34 of 48

Destructors

Are special methods that are used to release an instance of a class from memory. There are two types of destructors in Visual Basic .NET:

Finalize( )

Dispose( )

Page 35: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 35 of 48

Finalize ( ) Destructor

Is called from the class to which it belongs or from the derived classes.

Is called after the last reference to an object is released from the memory.

Is automatically invoked by the .NET Framework to destroy objects in the memory.

Page 36: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 36 of 48

Dispose ( ) Destructor

Is called to release a resource, such as a database connection, as soon as the object using such a resource is no longer in use.

Is not called automatically, and you must explicitly call it from a client application.

Page 37: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 37 of 48

Task 10: Write the code to store and retrieve data from the class.

Task 11: Save the application.

Task 12: Run the application.

Page 38: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 38 of 48

Problem Statement 3.D.2

The call centers at Diaz Telecommunications need data entry forms to store information in the Customers, Orders, Query Handling, Products, and Employees databases. Every data entry form should have a similar user interface with the Reset and Exit buttons. Incorporate the interface for the Order details form.

Page 39: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 39 of 48

Task List

Identify the mechanism to provide a similar user interface.

Identify the user interface components to accept data.

Identify the additional user interface controls.

Create the user interface screen, as designed.

Add code for the controls.

Perform the prerequisite task to implement the planned mechanism.

Implement the mechanism, as planned.

Page 40: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 40 of 48

Task List (Contd.)

Add the additional user interface control, as planned.

Perform the tasks to display a similar user interface.

Add code for the inherited controls.

Save the application.

Run the application.

Page 41: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 41 of 48

Task 1: Identify the mechanism to provide a similar user interface.

In Visual Basic .NET, you can create a base form in a project and then create a form that inherits from the base form.

Result:

Since the requirement stated in the problem statement is to create a similar user interface for all the data entry forms, you can create a base form that has the Reset and Exit

buttons on it. You can then inherit all the data entry forms from the base form.

Page 42: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 42 of 48

Task 2: Identify the user interface components to accept data.

Result:

Since all the data entry forms must have the Reset and Exit buttons, you need to design the user interface of the

base form with these two buttons. You also need to specify the Text and Name properties of the form and the buttons on the form.

Page 43: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 43 of 48

Task 3: Identify the additional user interface controls.

Result:

Since the data entry forms for the Customers, Orders, Query Handling, Products, and Employees databases must

have various controls to accept data from a user, you need to identify additional controls that need to be included in the derived forms. In the current scenario, you will only create a base form with two buttons and inherit the form

Order_details.

Page 44: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 44 of 48

Task 4: Create the user interface screen, as designed.

Task 5: Add code for the controls.

Task 6: Perform the prerequisite task to implement the planned mechanism.

Task 7: Implement the mechanism, as planned.

Task 8: Add the additional user interface control, as planned.

Page 45: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 45 of 48

Task 9: Perform the tasks to display a similar user interface.

Task 10: Add code for the inherited controls.

Task 11: Save the application.

Task 12: Run the application.

Page 46: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 46 of 48

Problem Statement 3.P.1

The call centers of Diaz Telecommunications need to have data entry forms to store information in the Customers, Orders, Query Handling, Product, and Employees databases. Every data entry form should have a similar user interface with the Add, Modify, Delete, Reset and Exit buttons. Incorporate the interface for the Query Handling data entry form.

Page 47: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 47 of 48

Summary

In this lesson, you learned that:

Visual Basic .NET is an object-oriented programming language.

A structure is used to create user-defined data types.

Data can be stored in and retrieved from a structure.

Abstract classes are used to define the skeleton of the methods that the derived class can implement.

Interfaces are inheritable in Visual Basic .NET.

Classes can be added to a Visual Basic .NET project.

Page 48: VB .net tutorial - 3

Implementing Object-oriented Programming in Visual Basic .NET

©NIIT Implementing Object-oriented Programming in VB .NET/Lesson 3/Slide 48 of 48

Summary (Contd.)

An assembly contains information necessary for deploying and maintaining the versions of an application.

An assembly consists of manifest, module, and type.

An important advantage of using a namespace is the prevention of a name collision.

Classes can be inherited in a Visual Basic .NET project.