vb .net tutorial - 4
TRANSCRIPT
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 1 of 34
Objectives
In this lesson, you will learn to:
Identify the different types of procedures in Visual Basic .NET
Identify the importance of procedure overloading
Identify the importance of procedure overriding
Implement the MsgBox( ) function
Implement the CommonDialog classes
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 2 of 34
Procedures
Are sets of one or more program statements that can be executed by referring to the procedure name.
Are the key to modular programming. They simplify the task of maintaining and debugging the application code.
Are useful for performing repetitive tasks, such as fetching specific records from a database, and text and control
manipulation.
Are of three types:
Sub procedures
Function procedures
Property procedures
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 3 of 34
Sub Procedure
Is a block of code enclosed within the Sub and End Sub statements.
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.
Example
Public Sub Check_Acct_Status (ByVal CustAcct As Integer, ByVal Amount As Single)
’The statements of the Sub procedure are given here.
End Sub
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 4 of 34
Sub Procedure (Contd.)
Can be defined in a module, a class, or a structure.
Can be created with one of the following access modifiers:
Public
Protected
Friend
Protected Friend
Private
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 5 of 34
Sub Procedure (Contd.)
Can take arguments. You declare each argument by specifying the argument name and the data type.
Can have an optional argument. An optional argument must be declared at the end of the argument list and must have a default value.
Can be called by using the Call keyword and providing values for all the arguments that are not optional. You can pass arguments to a sub procedure either by value or by reference.
Can be further categorized as:
General procedures
Event-handling procedures
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 6 of 34
Sub Procedure (Contd.)
General procedure
Is a block of code that performs a specific task.
Event-handling procedure
Is a block of code that is executed when a specific event occurs, such as the click of a button or the loading of a form in the memory.
Is a combination of the object name and the type of event that has occurred.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 7 of 34
Function Procedure
Is a block of code enclosed within the Function and End Function statements.
Returns a value to the calling code.
Example
Public Function Check_Acct_Status (ByVal CustAcct As Integer, ByVal Amount As Single) As Integer
' The statements of the Sub procedure are given here.
Return Amount
End Function
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 8 of 34
Function Procedure (Contd.)
Can be defined in a module, a class, or a structure.
Can be created with one of the following access modifiers:
Public, Protected, Friend, Protected Friend, and Private.
Has Public access by default.
Uses the Return statement to return a value to the calling procedure.
Return value can be trapped by calling a function and assigning the return value of the function to a variable.
Example
TotalAmount = Calculate_Amount(Total_Salary,months)
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 9 of 34
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.
Is a type of variable that stores the values of an object of a class or a structure.
Can help you define a property as read-only, write-only, or read/write type.
Is of two types:
Get procedures are used to retrieve the values from a property.
Set procedures are used to assign values to a property.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 10 of 34
Property Procedure (Contd.)
Is always invoked implicitly by the code that refers to the property, that is, the code uses the name of the property and provides values for all the arguments that are not optional.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 11 of 34
Passing Arguments to a Procedure
You can pass arguments to a procedure:
By value using the ByVal keyword
By reference using the ByRef keyword
In Visual Basic .NET, the default argument passing mechanism is ByVal.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 12 of 34
Parameter Array
Can be used to pass an array of values for an argument of a procedure.
Is defined by using the keyword ParamArray.
Rules:
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.
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.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 13 of 34
Parameter Array (Contd.)
The parameter array is optional. The default value of a parameter array is an empty one-dimensional array.
The parameter array must be the lone optional argument in the list of arguments for a procedure. All other
arguments preceding the parameter array must be used.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 14 of 34
Procedure Overloading
Means defining multiple procedures using the same name but different argument lists, also referred to as signature.
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 the arguments
The data types of the arguments
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 15 of 34
Procedure Overloading (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
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 16 of 34
Procedure Overriding
Means redefining a base class procedure in a derived class without changing the name of the procedure. Rules:
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.
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.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 17 of 34
Built-in Functions in Visual Basic .NET
Functions Usage
Len (string expression) To find the length of the string expression that is passed as the argument
Mid (string expression, starting position, number of characters)
To extract a particular number of characters, starting at a given character position, from the string expression that is passed as the argument
CDate (string expression) To convert the string expression that is passed as the argument to the Date type
CTime (string expression) To convert the string expression that is passed as the argument to the Time type
Val (string/object/ char expression) To convert the numbers contained in a string/char/object to a numeric value
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 18 of 34
Built-in Functions in Visual Basic .NET(Contd.)
CBool (expression) To convert a string or numeric expression to Boolean values
CByte (string/numeric expression) To convert a string or a numeric expression to a Byte data type
CInt (string/numeric expression) To convert a string or a decimal expression to an integer
CObj (string/numeric expression) To convert a string or numeric value to an object
CChar (string/numeric expression) To convert a string or numeric value to a Char
CStr (string/numeric expression) To convert a string or numeric value to a String
CDec (string/numeric expression) To convert a string or numeric value to a Decimal
CType(expression.datatype/object/class/structure)
To convert an expression from one type to another
Functions Usage
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 19 of 34
Problem Statement 4.D.1
The customer data entry forms at the call centers of Diaz Telecommunications need to include options to check for a valid customer ID and telephone number. The customer ID should start with the letter ‘C’ and have three digits after it. The telephone number should consist of eight digits. The form should also have the facility to display error messages when incorrect data is entered. Additionally, the form should have the provision to clear the last customer details entered.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 20 of 34
Task List
Identify the checks that need to be applied on the form.
Identify the changes needed in the design of the form.
Identify the mechanism to display error messages.
Add the additional controls to the form.
Check the data and display an error message.
Save the application.
Run the application to validate the checks applied on the form.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 21 of 34
Task 1: Identify the checks that need to be applied on the form.
Result:
As per the problem statement, the customer data entry form needs to have a provision to check for a valid customer id and telephone number.
You can add the provision for checking the validity of the customer id and the telephone number by including procedures in the customer data entry application.
Since Visual Basic .NET allows the use of procedure overloading, you can create two versions of a procedure in
the customer data entry application to check the validity of the customer id and the telephone number.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 22 of 34
Task 2: Identify the changes needed in the design of the form.
You will create an overloaded procedure for checking the validity of the customer id and the telephone number.
You can name the procedure Check_data ().
Result:
The following table recommends suitable prefixes that you can use for the two buttons:
Object Prefix Example
Button cmd cmdCheckdata
Button cmd cmdReset
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 23 of 34
Task 3: Identify the mechanism to display error messages.
Messages can be displayed by using either
Msgbox ()Function
MessageBox Class
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 24 of 34
Task 3: Identify the mechanism to display error messages. (Contd.)
Result:
To display an error message to users when an invalid customer id or telephone number is entered in the customers data entry form, you can use either the MessageBox class or the built-in MsgBox()function.
Since MessageBox class offers greater control over the interface of the message box, such as displaying appropriate icons, you should use the MessageBox class to display error message when invalid customer id or telephone number is entered.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 25 of 34
Task 4: Add the additional controls to the form.
Task 5: Check the data and display an error message.
Task 6: Save the application.
Task 7: Run the application to validate the checks applied on the form.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 26 of 34
Problem Statement 4.P.1
The Employee data entry form in the call centers at Diaz Telecommunications requires options to check for valid employee ID and age entries. The employee ID should start with the letter ‘E’ and have three digits after it. The age should be more than 20 years and less than 61 years. The form should also have the facility to display error messages when incorrect data is entered. Additionally, the form should have a provision to clear the last employee details entered. The details of an employee essentially include the employee id, the employee’s first name, the employee’s last name, the address, the age, the date of joining, the department, and the salary.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 27 of 34
Just a Minute…
1. There are two labels named Label1 and Label2 and one button named Button1 on the form, Form1. Predict the output of the code, when Button1 is clicked at run time.
2. There are two labels named Label1 and Label2 and one button named Button1 on the form, Form1. You have created a user-defined Sub procedure called MySub to process information when the user clicks Button1. Predict the output of the code, when the program is executed.
3. There is one label named Label1 and one button named Button1 on the form, Form1. What will be the output of the code when Button1 is clicked at run time?
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 28 of 34
Common Dialog Classes
Are used to access the default Font dialog box to change the font of the text or used to open a file by using the Open dialog box and display the contents.
Are of the following types:
ColorDialog
FontDialog
FileDialog
PrintDialog
PageSetupDialog
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 29 of 34
ColorDialog Class
Is used to change the background and the foreground color of text.
FontDialog Class
Is used to change the font, the font style, and the size of text.
FileDialog Class
Is an abstract class that is inherited from the CommonDialog class.
You cannot instantiate it directly. However, you can use the OpenFileDialog or SaveFileDialog class to open a file or save an existing file.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 30 of 34
PrintDialog Class
Is used to print text or graphics.
PageSetupDialog Class
Used to set the page details for printing in Windows applications.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 31 of 34
Just a Minute…
1. What will be the output of the code when Button1 is clicked at run time?
2. The code snippet is written in the Code Editor window of Form1. While building the project, there was a build error. What would you do to resolve the error?
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 32 of 34
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
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 33 of 34
Summary (Contd.)
Property procedure is of two types:
Get procedures are used to retrieve values from a property.
Set procedures are used to assign values to a property.
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.
Introducing Procedures and CommonDialog Classes in Visual Basic .NET
©NIIT Introducing Procedures and CommonDialog Classes in VB .NET/Lesson 4/Slide 34 of 34
Summary (Contd.)
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.