PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1...

19
PROGRAMMING IN VISUAL BASIC .NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8

Transcript of PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1...

Page 1: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

PROGRAMMING IN VISUAL BASIC .NET

VISUAL BASIC PROGRAMMING FUNDAMENTALS

Bilal Munir Mughal

1

Chapter-8

Page 2: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

In this chapter

Procedures Defined Working with Sub Procedures Working with Function Procedures Reusing Functions and Procedures

2

Page 3: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Procedures Defined

Procedures are segments of code that perform a particular task and then return processing to the area of the code from which they were called. This means that a single procedure can be called from multiple places in your code and, if managed properly, can be used with multiple programs

3

Page 4: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Types of Procedures

Sub Procedures perform a specific task. Sub procedures are often known simply as "procedures," and are denoted by the keyword Sub at the beginning of the procedure.

Function Procedures return a value to the code that called them. They are denoted by the keyword Function at the beginning of the procedure.

4

Page 5: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Types of Procedures

Event-Handling Procedures, or "event handlers," are invoked in response to an event having occurred to an object. Events can be triggered by the user or by the

program. Event handlers are normally identified by the name

of the object followed by an underscore and the name of the event that invokes the procedure. For example, btnExit_Click denotes the Click event handler for the Button control named btnExit.

Regardless of the procedure's name, however, the Handles keyword at the end of the procedure's definition declares which object/event combination is handled by the procedure.

5

Page 6: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Types of Procedures

Property Procedures are used when assigning values to the properties of user-created objects, and when retrieving the values of those properties. Property procedure definitions include the keyword Property.

6

Page 7: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Working with Sub Procedures

You can test each task individually. The smaller amount of code in a procedure makes debugging easier and makes working on the same project with other developers easier.

You can eliminate redundant code by calling a procedure each time a task needs to be performed instead of repeating the program code.

You can create a library of procedures that can be used in more than one program, saving yourself development time in new projects.

Program maintenance is easier for a couple of reasons. First, because code is not repeated, any edits to a block of code

must be performed only once. In addition, separating key components (for example, the user

interface and the database functions) allows you to make major changes in one part of the program without recoding the whole thing.

7

Page 8: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Procedure Location

Most often your procedure will be a method contained within a class, such as a Windows form class. In this case, you would enter the procedure within the form's class declaration (analogous to its Code window).

Procedures can also be declared in a module. A module is simply a file that contains Visual Basic code, but which is not associated with a particular form.

8

Page 9: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Working with Sub Procedures

Creating a Sub Procedure Executing the Procedure

Call procname([arguments]) procname([arguments])

Passing Data to a Procedure with Arguments Placing procedures in a separate module Optional Arguments Argument Passing Options

ByVal, which stands for by value ByRef, which stands for by reference

Exiting a Procedure Early

9

Page 10: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Working with Function Procedures

Function procedures are similar to sub procedures, with one key difference: They return a value. This means that somewhere in the body of the function procedure, a value will be calculated, retrieved, or otherwise created and set to be the function's return value. This value can subsequently be used by the calling code; typically, it is assigned to a variable or used in expressions.

Visual Basic offers a number of built-in functions that you can use, such as Val, which returns the numeric representation of a string, or Left, which returns a specified number of characters from the left end of a string.

Function procedures let you build your own custom functions as well.

10

Page 11: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Working with Function Procedures

Building a Function Invoking a Function

11

Page 12: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Understanding Scope and Accessibility

When you create a procedure, you might want to limit where it can be called from, where its variables can be accessed from, and how resources are allocated to make its code

available to other parts of your program. The scope of a variable or procedure refers to its

availability to be used by other parts of your code, and is determined by the context in which it is declared.

Scope is also known as visibility.

12

Page 13: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Scope of Variables

Block-Level Variables Procedure-Level Variables Module-Level Variables Project-Level Variables

Same-Named Variables Selecting a Scope Preserving Variables Between Procedure Calls

13

Page 14: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Scope of Procedures

Project-Level Procedures Class-Level Procedures

14

Page 15: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Accessibility of Procedures and Variables

Public Accessibility

A procedure or variable declared using the Public keyword can be used from any part of your project, but not from other projects.

Private Accessibility

Use of the Private keyword in a procedure or variable declaration limits the accessibility of the procedure or variable to the class (module) in which it is defined.

For variables, this only holds true if the variable is declared at module level. Variables declared inside a procedure are always private.

15

Page 16: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Accessibility of Procedures and Variables

Friend Accessibility

The Friend keyword declares a procedure or variable to be available across your entire assembly. The term assembly refers to a project or group of projects that is compiled, or assembled, together as one executable solution. Friend variables and procedures can be accessed by any code in any portion of the assembly.

Protected Accessibility

Protected variables and procedures, defined using the keyword Protected, are similar to private variables in that they are normally only available inside the class in which they are declared. However, if you derive a new class from an existing class that contains protected variables or procedures, the derived class can also access them.

16

Page 17: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Accessibility of Procedures and Variables

Protected Friend Accessibility

Finally, the Protected and Friend accessibilities can be combining by using the keyword combination Protected Friend. This expands the accessibility of a variable or procedure to both the entire assembly and derived classes.

17

Page 18: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

Reusing Functions and Procedures

Storing a Procedure in a Form File Using a Module File for Procedures

18

Page 19: PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.

19

Q & A?