190 likes | 235 Views
Programming in visual basic .net Visual Basic Programming Fundamentals. Bilal Munir Mughal. Chapter-8. In this chapter. Procedures Defined Working with Sub Procedures Working with Function Procedures Reusing Functions and Procedures. Procedures Defined.
E N D
Programming in visual basic .netVisual Basic Programming Fundamentals Bilal Munir Mughal Chapter-8
In this chapter • Procedures Defined • Working with Sub Procedures • Working with Function Procedures • Reusing Functions and Procedures
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
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.
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.
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.
Working with Sub Procedures • 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 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 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.
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.
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, stands for by value • ByRef, stands for by reference • Exiting a Procedure Early
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.
Working with Function Procedures • Building a Function • Invoking a Function
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.
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
Scope of Procedures • Project-Level Procedures • Class-Level Procedures
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.
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.
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.
Reusing Functions and Procedures • Storing a Procedure in a Form File • Using a Module File for Procedures
Q & A ?