You are on page 1of 48

Introducing Procedures and CommonDialog

Classes in Visual Basic .NET

Pre-Assessment Questions
1. Which of them is not an advantage of using classes and objects
a. Maintenance of code
b. Encapsulation of internal complexities.
c. Reusabilty
d. Modularity

2. The methods that are used to release the instance of a class are
a. Destructors
b. Constructors
c. Dispose method
d. Finalize method

©NIIT Introduction to VB .NET Lesson 2B / Slide 1 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Pre-Assessment Questions (Contd.)


3. Which feature refers to sharing of attributes and behaviors?
a. Polymorphism
b. Encapsulation
c. Abstraction
d. Inheritance

4. Which of the following is false


a. Namespaces are a naming scheme.
b. Interfaces are not inheritable in VB .NET.
c. The concept of using operators or functions in different ways depending
on what they are operating on is called polymorphism.
d. Inheritance of classes allow you to reuse code.

©NIIT Introduction to VB .NET Lesson 2B / Slide 2 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Pre-Assessment Questions (Contd.)


5. Which of the statements cannot be enclosed within the Interface and End
Interface statements?
a. Event
b. Property
c. Function
d. Sub property

©NIIT Introduction to VB .NET Lesson 2B / Slide 3 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Solutions to Pre-Assessment
Questions
1. d. Modularity
2. a. Destructors
3. d. Inheritance
4. b.
5. d. Sub property

©NIIT Introduction to VB .NET Lesson 2B / Slide 4 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Objectives
In this lesson, you will learn to:
• Declare different types of procedures in Visual Basic .NET
• Implement procedure overloading
• Implement procedure overriding
• Implement the MsgBox() function
• Implement the CommonDialog classes
• Work with Menus
• Work with MDI Applications

©NIIT Introduction to VB .NET Lesson 2B / Slide 5 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Getting Started with Procedures


• A procedure is a set of one or more program statements that can be executed
by referring to the procedure name.

©NIIT Introduction to VB .NET Lesson 2B / Slide 6 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

An Introduction to Procedures
• While developing an application, you may need to use the same code in
multiple parts of the application. Repeating code often leads to problems in
maintaining it, to avoid such situations, you can use procedures

©NIIT Introduction to VB .NET Lesson 2B / Slide 7 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

An Introduction to Procedures(Contd.)
• A procedure is a set of one or more program statements that can be executed
by referring to the procedure name.
• You can reuse the code written in a procedure
• Procedures are useful for performing repetitive tasks, such as fetching specific
records from a database, and text and control manipulation.
• The use of procedures provides the following benefits:
• Procedures allow you to break an application into discrete logical units,
thus making the application more readable.
• Procedures help in debugging an application because debugging separate
units of an application is easier than debugging the application as a whole.
• Procedures are reusable across programs with little or no modification.

©NIIT Introduction to VB .NET Lesson 2B / Slide 8 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

An Introduction to Procedures(Contd.)
• There are three types of procedures in Visual Basic .NET:
• Sub procedures
• Function procedures
• Property procedures

©NIIT Introduction to VB .NET Lesson 2B / Slide 9 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Sub Procedures
• A Sub procedure is a block of code enclosed within the Sub and End Sub
statements.
• The execution starts with the first executable statement after the Sub
statement and ends with the first End Sub, Exit Sub, or Return statement.
• A Sub procedure does not return a value. However, it can take arguments,
such as constants, variables, and expressions that are passed to it by the
calling code.
• The access modifiers that can be used with Sub procedures are Public,
Protected, Friend, Protected Friend, and Private.
• Each argument can be declared for a procedure by specifying the argument
name and the data type
• You can pass arguments to a Sub procedure either by value or by reference

©NIIT Introduction to VB .NET Lesson 2B / Slide 10 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Sub Procedures (Contd.)


• Calling a Sub Procedure
• Sub procedure can be called by using the procedure name.
• Sub procedures are of the following two types:
• General procedures
• Event-handling procedures

©NIIT Introduction to VB .NET Lesson 2B / Slide 11 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Function Procedures
• A Function procedure is a block of code enclosed within the Function and End
Function statements.
• A Function procedure, unlike a Sub procedure, returns a value to the calling
code.
• The argument declaration for a Function procedure is similar to that for a Sub
procedure.
• Return Values of a Function Procedure
• Return values are the values that a Function procedure sends back to the
calling code.
• A Function procedure uses the Return statement to specify the return
value.
• To trap the returned value of a Function procedure, you need to call the
Function procedure by including its name and arguments on the right side of
an assignment statement.

©NIIT Introduction to VB .NET Lesson 2B / Slide 12 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Property Procedure
• A Property procedure is a set of code statements that are used to assign or
retrieve the values of the properties declared within a module, a class, or a
structure.
• You can access properties either as Public variables or by using the Set and
Get Property procedures.
• There are two types of Property procedures in Visual Basic .NET.
• Get procedures are used to retrieve the values from a property.
• Set procedures are used to assign values to a property.

©NIIT Introduction to VB .NET Lesson 2B / Slide 13 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Property Procedure (Contd.)


• A property is defined by a set of code enclosed within the Property and End
Property statements.
• For a Property procedure, an argument must be passed by value and not by
reference.
• A Property procedure is always invoked implicitly by the code that refers to the
property.

©NIIT Introduction to VB .NET Lesson 2B / Slide 14 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Parameter Array
• Using a parameter array, you can pass an array of values for an argument of
a procedure.
• The ParamArray keyword is used to denote a parameter array.
• Rules to Follow while Using a Parameter Array
• You cannot use more than one parameter array in a procedure, and it
must be the last argument in the procedure definition.
• The parameter array must be passed by value. You should specify
the ByVal keyword.
• The code within the procedure must use the parameter array as a
one-dimensional array. In addition, each element of the array must
be of the same data type as the data type of ParamArray.
• The parameter array is optional. The default value of a parameter
array is an empty one‑dimensional array.
• The parameter array must be the only one optional argument in the
list of arguments for a procedure. All other arguments preceding the
parameter array must be assigned value.

©NIIT Introduction to VB .NET Lesson 2B / Slide 15 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Parameter Array (Contd.)


• Passing Arguments for a Parameter Array Inheritance
• While calling a procedure, you can pass the following arguments for a
parameter array:
• An array that has the same element type as the parameter array.
• A list of an indefinite number of arguments. The arguments are separated
by commas.

©NIIT Introduction to VB .NET Lesson 2B / Slide 16 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Procedure Overloading and Procedure


Overriding
• Procedure overloading is defining multiple procedures using the same name
but different argument lists. An interface defines properties, methods, and
events.
• When you overload a procedure:
• Each overloaded version uses the same procedure name.
• Each overloaded version differs from all the other overloaded versions in
one of the following ways:
• The number of arguments
• The order of arguments

©NIIT Introduction to VB .NET Lesson 2B / Slide 17 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Procedure Overloading and Procedure


Overriding (Contd.)
• You cannot overload a procedure by varying only one or more of the following
items:
• The procedure modifiers, such as Public, Shared, and Static.
• The argument names
• The argument modifiers, such as ByRef and Optional
• The data type of the return value

©NIIT Introduction to VB .NET Lesson 2B / Slide 18 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Procedure Overriding
• Procedure overriding can be performed in Visual Basic .NET application.
• You need to keep in mind the following rules while implementing overriding of
procedures:
• You can override procedures that are declared with the Overridable
keyword in the base class.
• You need to explicitly declare a procedure in the base class with the
Overridable keyword in order to override it in the derived class. This is
because procedures are not Overridable by default.
• Overridden procedures need to have the same arguments as the inherited
members from the base class.
• The redefined implementation of a procedure in the derived class can call
the implementation defined in the parent class by specifying MyBase
keyword before the procedure name.

©NIIT Introduction to VB .NET Lesson 2B / Slide 19 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Implementing the MsgBox() function


• The Msgbox() function is one of the built-in functions in Visual Basic .NET and
allows you to display messages in a dialog box.
• The Msgbox() function takes three arguments of which the first is a required
argument and the last two are optional. The arguments are as follows:
• Prompt -It contains a string expression that is to be displayed as the
message in the dialog box.
• Buttons – It contains a numeric expression that is the sum of the values
specifying the number and type of buttons to display, the icon style to
use, and the identity of the default button.
• Title – It contains a string expression that is displayed as the title in the
title bar of the dialog box.

©NIIT Introduction to VB .NET Lesson 2B / Slide 20 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Implementing the MsgBox() function


(Contd.)
• MessageBox Class
• The MessageBox class can be used to display informative messages to
users.

©NIIT Introduction to VB .NET Lesson 2B / Slide 21 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Implementing the MsgBox() function


(Contd.)
The following example illustrates the use of the MessageBox class to display a
message: Message Box With Icon
MessageBox.Show("The entered data is not valid. Please enter valid
data.", "Data Entry Error",MessageBoxButtons.OK,
MessageBoxIcon.Error)

Message Box With


Icon
 

©NIIT Introduction to VB .NET Lesson 2B / Slide 22 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Demo for
Creating Procedures in Visual Basic
.NET

©NIIT Introduction to VB .NET Lesson 2B / Slide 23 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Problem Statement
• Create the customer data entry that was created in the previous session to
accept the details of the customer such as CustomerID, First Name, Last
Name, Address, Telephone number and E-mail Id. The application needs
to include options to check for a valid customer ID and telephone number. The
customer ID should start with the letter 'C' and have four digits after it. The
telephone number should consist of only digits (no alphabets). The form should
also have the facility to display error messages when incorrect data is entered.

©NIIT Introduction to VB .NET Lesson 2B / Slide 24 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Solution

• To solve the above problem you need to perform the following steps:
• Adding controls to the form.
• Checking of data and displaying an error message.
• Saving and executing the application.

©NIIT Introduction to VB .NET Lesson 2B / Slide 25 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Introducing CommonDialog Classes


• If you want to access the default Font dialog box to change the font of text or
use the Open dialog box to open a file and display the contents, you need to
use the inherited classes of the CommonDialog class.
• All the classes that inherit from the CommonDialog class override the
RunDialog() function to create a specific common dialog box.
• The classes that are inherited from the CommonDialog class are ColorDialog,
FontDialog, FileDialog, PrintDialog, and PageSetupDialog.

©NIIT Introduction to VB .NET Lesson 2B / Slide 26 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

CommonDialog Classes
• VB.NET provides the following types of common dialog classes:
• ColorDialog class
• OpenFileDialog class
• SaveFileDialog class
• PrintDialog class

©NIIT Introduction to VB .NET Lesson 2B / Slide 27 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

ColorDialog Class
• To change the background and the foreground color of text, you can use the
default Color dialog box.
• To use the default Color dialog box, you can either add a ColorDialog control
to the form or create an instance of the inherited ColorDialog class.
• The ColorDialog Class is as shown below:

©NIIT Introduction to VB .NET Lesson 2B / Slide 28 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

FontDialog Class
• The default Font dialog box can be used to change the font, the font style,
and the size of text.
• To use the default Font dialog box, you can either add the FontDialog
control to the form or instantiate the FontDialog class.

©NIIT Introduction to VB .NET Lesson 2B / Slide 29 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

FontDialog Class (Contd.)


• Font dialog box is as shown below. The Font Dialog Box

The Font Dialog Box

©NIIT Introduction to VB .NET Lesson 2B / Slide 30 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

FileDialog Class
• The FileDialog class is an abstract class that is inherited from the
CommonDialog class.
• Since the FileDialog class is an abstract class, you cannot instantiate it
directly.
• The OpenFileDialog and SaveFileDialog classes are inherited from the
FileDialog class.

©NIIT Introduction to VB .NET Lesson 2B / Slide 31 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

OpenFileDialog Class
• If you want to invoke the default Open dialog box to open a file in the Visual
Basic .NET environment, you either need to add an OpenFileDialog control
to the form or instantiate the OpenFileDialog class.
• The Open dialog box is as shown below

©NIIT Introduction to VB .NET Lesson 2B / Slide 32 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

SaveFileDialog Class
• If you want to invoke the default Save As dialog box to overwrite an existing
file or create a new file using the Visual Basic .NET environment, you either
need to add a SaveFileDialog control to the form or instantiate the
SaveFileDialog class.
• Save As dialog box is as shown below

©NIIT Introduction to VB .NET Lesson 2B / Slide 33 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

PrintDialog Class
• In the Visual Basic .NET environment, you can use the default Print dialog
box to print any text or graphics.
• To invoke the default Print dialog box, you need to either add a PrintDialog
control and a PrintDocument control to the form or create an instance of
the PrintDialog and PrintDocument classes.
• PrintDialog box is as shown below

©NIIT Introduction to VB .NET Lesson 2B / Slide 34 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

PageSetupDialog Class
• In the Visual Basic .NET environment, you can use the default Windows
Page Setup dialog box to set the page details for printing in Windows
applications.
• To invoke the default Windows Page Setup dialog box, you need to either
add a PrintSetupDialog control to the form or create an instance of the
PrintSetupDialog class.

©NIIT Introduction to VB .NET Lesson 2B / Slide 35 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

PageSetupDialog Class (Contd.)


The PageSetup Dialog Box is as shown below

©NIIT Introduction to VB .NET Lesson 2B / Slide 36 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

PrintDocument Class
• In The .NET framework provides the PrintDocument class to support printing
.NET applications.
• Following are the two ways of using the PrintDocument class in an .NET
application:
• Add the PrintDocument object to the form, from the toolbox at the
design time.
• Declare an instance of the PrintDocument class at run time.

©NIIT Introduction to VB .NET Lesson 2B / Slide 37 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Working with Menus and MDI


applications
• VB.NET provides support to create menus and MDI applications
• You can also display multiple forms at the same time. This functionality is
called MDI.

©NIIT Introduction to VB .NET Lesson 2B / Slide 38 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Working with Menus


• In the Windows environment, you can use menus to enhance the user
interface of an application.
• Menus Appearing on the Menu Bar
• The menus that appear on the menu bar contain a list of options that a
user can use for various requirements.For example, in a word
processing application, the File menu has options, such as Open,
Save, and Close
• In Visual Basic .NET, the menus that appear on the menu bar are
created using the MainMenu object. Declare an instance of the
PrintDocument class at run time.

©NIIT Introduction to VB .NET Lesson 2B / Slide 39 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Working with Menus (Contd.)


• In Context Menus
• Visual Basic .NET supports the creation of context menus to provide
users easy access to related options.
• Context menu items can also be added to a form either at design time
or at run time
• When you add a MainMenu control or a ContextMenu control from
the Toolbox, the control is added to the Component tray, which appears
at the bottom of the form.

©NIIT Introduction to VB .NET Lesson 2B / Slide 40 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Working with Menus (Contd.)


• The following figure displays the Component tray that holds a MainMenu
control and a ContextMenu control.

©NIIT Introduction to VB .NET Lesson 2B / Slide 41 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Working with MDI Applications


• Visual Basic .NET supports Multiple Document Interface (MDI) applications.
• When an application supports multiple windows simultaneously, it is called
an MDI application.
• In Visual Basic .NET, an MDI application consists of two parts, MDI parent
form and MDI child form.

©NIIT Introduction to VB .NET Lesson 2B / Slide 42 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Demo for
Integrating the Parts of an Application

©NIIT Introduction to VB .NET Lesson 2B / Slide 43 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Problem Statement
• Create a data entry application. This application should enable users to
access multiple data entry forms. To allow easy access to different data entry
forms, the data entry application should provide a user-friendly interface. It
should also allow users to access the monthly sales report for data analysis.
In addition, the users should be able to exit the data entry application when
required. The forms that the users should be able to access are Customer
Details form, Employee Details form, and Order Details form.

©NIIT Introduction to VB .NET Lesson 2B / Slide 44 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Solution
• Designing the required integration.
• Integrate the application.
• Saving and executing the application

©NIIT Introduction to VB .NET Lesson 2B / Slide 45 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Summary
In this lesson, you learned that:
• A procedure is a set of one or more program statements that can be
executed by referring to the procedure name.
• Procedures are of three types:
• Sub
• Function
• Property
• Sub procedures can be of two types:
• General
• Event-handling
• Property procedures are of two types:
• Get procedures are used to retrieve values from a property.
• Set procedures are used to assign values to a property.

©NIIT Introduction to VB .NET Lesson 2B / Slide 46 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Summary (Contd.)
In this lesson, you learned that:
• Property procedures are used to access properties declared within a module,
a class, or a structure.
• Arguments can be passed to a procedure either by value or by reference.
• Parameter arrays enable you to pass an array of values as an argument to a
procedure.
• Procedure overloading is defining multiple procedures having the same
name but different argument lists.
• Procedure overriding enables you to redefine a base class procedure in a
derived class without changing the name of the procedure.
• The CommonDialog class is the base class for the most commonly used
dialog boxes, such as, Font, File, Print, and Page Setup.
• MDI applications enable you to integrate different parts of an application.

©NIIT Introduction to VB .NET Lesson 2B / Slide 47 of 27


Introducing Procedures and CommonDialog
Classes in Visual Basic .NET

Summary (Contd.)
• Menus are used to provide a user-friendly interface for accessing
options.
• Context menus enable users to access the most frequently used
options.

©NIIT Introduction to VB .NET

You might also like