400 likes | 410 Views
Chapter 7. INTRODUCTION TO VB .NET. Objectives. In this chapter, you will: Learn about the VB .NET programming language Learn how to declare variables and assign values to variables Use the VB .NET Debugger to monitor program execution Learn how to create user-defined procedures.
E N D
Chapter 7 INTRODUCTION TO VB .NET Database-Driven Web Sites, Second Edition
Objectives In this chapter, you will: • Learn about the VB .NET programming language • Learn how to declare variables and assign values to variables • Use the VB .NET Debugger to monitor program execution • Learn how to create user-defined procedures Database-Driven Web Sites, Second Edition
Objectives In this chapter, you will: • Work with complex numeric and string expressions in VB .NET • Create decision and repetition (looping) structures • Create and reference object classes • Learn about the VB .NET collection class Database-Driven Web Sites, Second Edition
Introduction to VB .NET • VB .NET: • Full-featured object-oriented programming language • Strongly typed programming language • Commands are not case-sensitive • The dot notation is used to reference program objects, properties, and methods Database-Driven Web Sites, Second Edition
Introduction to VB .NET • VB .NET program commands end when the Enter key is pressed to move to a new line in the Code editor • To continue a VB .NET command beyond one line, use the line continuation character: a blank space followed by a single underline character (_) Database-Driven Web Sites, Second Edition
VB .NET Variables and Assignment Statements • Programs use variables to store and reference values such as numbers, text strings, dates, and other types of data • When a variable is declared, the system sets aside space in main memory that contains information about the variable Database-Driven Web Sites, Second Edition
VB .NET Data Types • Every variable has a data type that specifies the kind of data that the variable stores • Four basic non-numeric data types: Boolean, Date, Object, and String Database-Driven Web Sites, Second Edition
VB .NET Variable Names • Every variable must be given a name • Variable names: • Can be a maximum of 255 characters long • Can contain letters, numbers, or underscores (_) • Cannot begin with a number Database-Driven Web Sites, Second Edition
Assigning Values to Variables • Assignment statement: • Assigns a value to a variable • Syntax:variable_name assignment_operator expression • Simple expression: a literal value • Complex expression: • multiple literal values • variables • built-in or user-defined functions • object properties connected by arithmetic or concatenation operators Database-Driven Web Sites, Second Edition
VB .NET Variable Persistence and Scope • Variable’s persistence: how long a variable is available to commands that need to reference or modify it • Variable’s scope: the location of the commands that can reference and modify a variable • Local variable: declared within a procedure using the Dim declaration command Database-Driven Web Sites, Second Edition
VB .NET Variable Persistence and Scope • Local variable: can only be accessed by commands in the procedure that declares it • Class variable: • Declared within a code behind file’s Declarations block using the Private declaration command • Visible to all procedures within the code behind file, but not visible to procedures within other classes in the Web application project Database-Driven Web Sites, Second Edition
Using the VB .NET Debugger • Debugger: • helps find and correct errors • allows setting breakpoints to pause execution on specific program lines • provides multiple ways to view variable values during program execution • If the mouse pointer is placed on the variable in the Code editor, the variable’s value appears in a ScreenTip • Watch: can monitor the variable’s value during execution Database-Driven Web Sites, Second Edition
Creating VB .NET Procedures • Procedures: • Self-contained code blocks that commands in other programs can call and pass parameters to • Make the code more modular and easier to maintain and debug • Can be subroutines or functions Database-Driven Web Sites, Second Edition
Subroutines • Subroutine: a procedure that manipulates variable values but does not return a specific value to the calling program • Figure 7-6 shows the general syntax of a VB .NET subroutine Database-Driven Web Sites, Second Edition
Functions • Function: a procedure that manipulates variable values and returns a specific value to the calling program • Figure 7-8 shows the general syntax of a VB .NET function Database-Driven Web Sites, Second Edition
Scope of Procedures • Procedures have scopes that limit the location of commands that can call the procedures • Scope can be • Public: commands in other code modules can call the procedure and commands in custom classes can call the procedure • Private: only commands within the code behind file where the procedure is declared can call the procedure Database-Driven Web Sites, Second Edition
Scope of Variables in Procedures • Local variable: visible only within the procedure that declares it • Commands within other procedures cannot reference a procedure’s local variables • Class variable: is visible to all procedures in the Web form’s code behind file • Two or more Web form variables can have the same name as long as the scope of the variables is different Database-Driven Web Sites, Second Edition
Persistence of Variables in Procedures • Persistence: the time between when a variable is declared and when the system destroys the variable • Local variable persists from the time the variable is declared to the time that the procedure in which the variable is declared ends • Class variable persists as long as the form in which it was declared is running Database-Driven Web Sites, Second Edition
Passing Parameters to Procedures • When a procedure is created, a parameter list is specified in the procedure declaration • Parameter list: enables the command that calls the procedure to pass data values to the procedure • Parameters can be: • Literal values, such as the number 10 or the string “Paula Harris” • Variables Database-Driven Web Sites, Second Edition
Passing Parameters to Procedures • When a procedure is called, the parameters are passed to the procedure by listing the parameter values in the same order in which the parameters appear in the procedure declaration • Each parameter value must have the same data type as its associated parameter in the procedure declaration Database-Driven Web Sites, Second Edition
Working with Complex Expressions in VB .NET • Simple expression: a literal value such as the number 3, a variable, or a value returned by a function • Complex expression: combines multiple simple expressions using arithmetic or concatenation operators Database-Driven Web Sites, Second Edition
Creating Complex Numeric Expressions • Complex numeric expressions can be created in VB .NET using arithmetic operators • Multiple operations can be combined in a single command • Operations can be nested • Overflow error: occurs if a developer attempts to assign a numeric value to a variable but the value is too large for the variable’s data type Database-Driven Web Sites, Second Edition
Creating Complex String Expressions • Concatenation operator (&): creates a single string value by joining two or more string expressions • VB .NET supports several built-in string functions and methods to support string operations • ToUpper and ToLower: convert a string to all uppercase characters or to all lowercase characters Database-Driven Web Sites, Second Edition
Creating Complex String Expressions • Parse: to extract one or more characters from a string and represent the extracted characters as separate string variables • Plus sign (+) can be used for concatenation, but this usage should be avoided • The plus sign can cause errors or unpredictable results when concatenating string values that contain numeric expressions Database-Driven Web Sites, Second Edition
Converting Data Types in VB .NET • It is a good practice to convert a value to the correct data type explicitly before it is used in an expression that involves a numeric or string operation • The data types of VB .NET variables and object properties can be converted using the VB .NET data conversion functions and the ToString string conversion method Database-Driven Web Sites, Second Edition
Converting Data Types in VB .NET • VB .NET has several built-in functions that perform data conversion operations: new_value = Function (expression) • The ToString method can be used to convert non-string values to the String data type • The general syntax of the ToString method is:value.ToString Database-Driven Web Sites, Second Edition
VB .NET Decision Control and Repetition (Looping) Structures • Decision control structure: enables a program to execute alternate statements based on a condition that is either true or false • Repetition (looping) structure: processes multiple values the same way until an exit condition is true • The general syntax of the VB .NET If control structure is:If condition Thenprogram_statementsEnd If Database-Driven Web Sites, Second Edition
VB .NET Decision Control and Repetition (Looping) Structures • Sometimes in a decision control structure, the program needs to execute one set of commands if the condition is true and an alternate set of commands if the condition is false • To create this kind of a decision control structure, the If/Else form of the If decision control structure is used Database-Driven Web Sites, Second Edition
VB .NET Decision Control and Repetition (Looping) Structures • If/ElseIf form: allows you to create an If decision control structure that evaluates multiple different conditions and executes different commands for each condition • Select Case control structure: tests multiple conditions that compare the same variable value and then performs associated programming commands Database-Driven Web Sites, Second Edition
VB .NET Decision Control and Repetition (Looping) Structures • AND operator: both conditions must be true for the overall condition to be true • OR operator: either condition must be true for the overall condition to be true • NOT operator: used to execute commands if a condition is not true Database-Driven Web Sites, Second Edition
Creating Looping Structures • A loop • Repeatedly processes statements the same way • Executes a series of program statements and periodically evaluates an exit condition that determines whether the loop repeats or exits • Pretest loop: evaluates the exit condition before any program commands execute • Posttest loop: executes one or more program commands before evaluating the exit condition the first time Database-Driven Web Sites, Second Edition
Creating Custom Classes in VB .NET • Object: • Abstract representation of something in the real world • Has properties and methods that specify its behavior • Object class: defines the properties and actions of similar objects • Custom classes: programmers define specific properties and methods that are not built-in • All of the project’s Web forms can use the custom class’s methods Database-Driven Web Sites, Second Edition
Defining Class Properties and Methods • Class definition file: where variables that specify the class’s properties are declared • Procedures that specify the class’s methods also need to be created Database-Driven Web Sites, Second Edition
Creating Class Instances and Calling Class Methods • The next steps in using a custom class are to create a new object instance of the class in a Web form and use the object to call the class methods • Instantiating the class: creating an object that is a member of a class • Local object: visible only within the procedure that creates it • Module-level object: visible to all procedures within the Web form that creates it Database-Driven Web Sites, Second Edition
Creating Class Instances and Calling Class Methods • To create local and module-level objects:Dim object_name As New project_name.class_namePrivate object_name As New project_name.class_name • To call a class method from a Web form, the following general syntax is used:object_name.method_name (parameter1, parameter2, ...) Database-Driven Web Sites, Second Edition
Class Inheritance • In object-oriented programming, a class can inherit properties and methods from a base, or parent, class • In VB .NET, a developer can create a custom class that inherits properties and methods from a base class • To reference a property or method in a parent class, the property or method is prefaced with the MyBase keyword, using the following dot syntax:MyBase.name Database-Driven Web Sites, Second Edition
The VB .NET Collection Class • Collection class: an object class with methods that can be used to manipulate class objects in VB .NET programs • A developer can instantiate a collection and then add objects to the new collection • Collection element: each individual object within a collection Database-Driven Web Sites, Second Edition
The VB .NET Collection Class • To instantiate a collection:Private collection_name As New Collection() • To access individual objects within a collection, use a For Each/Next loop • For Each/Next loop: sequentially steps through the objects in a collection and allows the developer to manipulate them Database-Driven Web Sites, Second Edition
Summary • VB .NET • Full-featured object-oriented programming language • Supports several numeric and non-numeric data types • Assignment statements: assign values of simple or complex expressions to variables • A variable’s persistence: how long the variable remains in memory and when commands can reference the variable and modify its value Database-Driven Web Sites, Second Edition
Summary • Variable’s scope: the location of the commands that can reference and modify the variable • Decision control structures can be created using the If, If/ElseIf, and Select Case decision structures • Looping structures can be created using Do loops, Do While loops, and For/Next loops • VB .NET allows programmers to create custom object classes, add object instances to these classes, and create associated object methods Database-Driven Web Sites, Second Edition