Professional Documents
Culture Documents
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
Solutions to Pre-Assessment
Questions
1. d. Modularity
2. a. Destructors
3. d. Inheritance
4. b.
5. d. Sub property
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
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
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.
An Introduction to Procedures(Contd.)
• There are three types of procedures in Visual Basic .NET:
• Sub procedures
• Function procedures
• Property procedures
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
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.
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.
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.
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.
Demo for
Creating Procedures 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.
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.
CommonDialog Classes
• VB.NET provides the following types of common dialog classes:
• ColorDialog class
• OpenFileDialog class
• SaveFileDialog class
• PrintDialog class
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:
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.
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.
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
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
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
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.
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.
Demo for
Integrating the Parts of an Application
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.
Solution
• Designing the required integration.
• Integrate the application.
• Saving and executing the application
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.
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.
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.