Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Right from the Start with Visual Basic .NET 1/e

Similar presentations


Presentation on theme: "Programming Right from the Start with Visual Basic .NET 1/e"— Presentation transcript:

1 Programming Right from the Start with Visual Basic .NET 1/e
9 Procedures and Arrays Programming Right from the Start with Visual Basic .NET 1/e

2 Objectives Understand the different types of variable scope
Understand the three types of procedures in VB. NET Explain the similarities and differences between function and sub procedures

3 Objectives (cont.) Explain the relationship between arguments and parameters Distinguish between an array element and an array index Understand how to declare, reference, and resize an array

4 Objectives (cont.) Understand how to declare and use a structure array
Understand how to declare and use a control array Understand how to work with KeyPress events Successfully write Windows applications involving procedures and arrays

5 9-1 Understanding Procedures
A procedure is a block of statements that begins with a declaration statement and concludes with an End statement. Procedures allow for modular programming, a methodology whereby long programs are divided into numerous small procedures that are based on logical activities.

6 9-1 Understanding Procedures (cont.)
Information can be passed to and from procedures by means of arguments and parameters. An argument is a value passed from the calling code to the procedure. A parameter is a variable listed in the formal procedure declaration that receives the argument.

7 9-2 Event Procedure An event procedure (or event handler) is a procedure containing code that is executed in response to an event. The sender parameter provides a reference to the object that raised the event. The second parameter is an object whose type depends on the event that is being handled.

8 9-3 Sub Procedure A sub procedure (or subroutine) contains developer code to perform a logical action. A sub procedure can be used as a container for code that is called multiple times. To call a sub procedure, reference the sub procedure name.

9 9-4 Function Procedure A function procedure (or function) is identical to a sub procedure, except that a function returns a value. The value the function sends back is called its return value. The Return statement is used to return a value from a function and return control back to the code that called the function.

10 9-5 Variable Scope When you declare a variable, you also define its scope. The scope of a variable is the region of code in which that variable may be directly referenced. It is determined by where you place the declaration and what keywords you use to declare it.

11 Local Scope A local variable is a variable declared inside a procedure. Local variables can only be referenced within the defining procedure. A local variable is destroyed when the procedure is finished executing.

12 Module Scope A module variable is declared at the module level outside of any procedure. Module scope variables are typically denoted by adding a lowercase m to the beginning of the variable name. Module level variables may be referenced by any procedure in the module.

13 9-6 Working with Arrays An array is a variable that holds multiple values. The values stored in an array are called the elements of the array. Each element in the array is distinguished by a unique number called the index (or subscript).

14 9-6 Working with Arrays (cont.)
The first element of an array is always index 0. The last element of the array is called the upper bound and can be obtained by the UBound function. The number of elements in the array is always one more than the upper bound and can be obtained by the Length method.

15 Declaring an Array Array declarations use the keyword Dim followed by the array name followed by the array’s upper bound in parentheses. Dim ArrayName(upperbound) As datatype The elements of an array must all be of the same type, and this type is called the element type of the array.

16 Referencing an Array To reference an element of an array, you must specify both the name of the array and the index into the array. ArrayName(index) It is common to use For…Next loops and arrays together.

17 ReDim Statement After you declare an array, you can change its size (but not its type) by using the ReDim statement. ReDim [Preserve] ArrayName(NewArraySize) You will typically use ReDim to make an array larger. You can use the Preserve keyword to save the current contents.

18 Array.Sort Method When you declare an array variable, it inherits functionality from a general Array class. The Array.Sort method will sort the elements of an array.

19 9-7 Structure Arrays An array is a variable that holds multiple elements of the same type. A structure provides a means of combining several different variables into a single type, which can then be stored as an array element. Each variable declared inside the structure is called a member.

20 9-8 Control Arrays A control array is an array of Windows Form controls such as text boxes, labels, or buttons. Control arrays allow a developer to write a small amount of code that affects a potentially large number of GUI elements. Dim ArrayName(arraysize) As ControlType

21 9-9 KeyPress Event The KeyPress event is useful for capturing keystroke data as it would appear in a text box. The second argument, e, is a KeyPressEventArgs object, which exposes only two properties: Handled and KeyChar.

22 9-9 KeyPress Event (cont.)
The KeyChar property is the character that corresponds to the key pressed. Handled is a Boolean value that you set to True to tell the form engine that you have already processed the event and that the engine should take no further action.

23 Chapter Summary A procedure is a set of instructions that perform a specific task. An argument is a means of sharing information between the calling program and a procedure. Modular programming involves breaking your application into procedures of logical units.

24 Chapter Summary (cont.)
There are three types of procedures: event procedures, sub procedures, and function procedures. Event procedures contain code for responding to user or system events. Sub procedures are created by the developer to perform a logical series of actions.

25 Chapter Summary (cont.)
Functions are identical to subroutines, except that functions return values. A variable declared in a procedure has local scope and can only be referenced within that procedure. A variable declared outside of any procedure is a module level declaration.

26 Chapter Summary (cont.)
An array is a variable that holds multiple values. After an array has been declared, its size can be changed using the ReDim statement. The Array.Sort method will sort the contents of an array.

27 Chapter Summary (cont.)
A structure provides a means of combining several different variables into a single type. A control array is an array of controls, such as buttons or labels. The KeyPress event is good for capturing keystroke data as it would appear in a text box.

28 Programming Right from the Start with Visual Basic .NET 1/e
9 Procedures and Arrays Programming Right from the Start with Visual Basic .NET 1/e


Download ppt "Programming Right from the Start with Visual Basic .NET 1/e"

Similar presentations


Ads by Google