microsoft visual basic 2005 chapter 4 variables and arithmetic operations

70
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations

Upload: martina-smith

Post on 02-Jan-2016

232 views

Category:

Documents


1 download

TRANSCRIPT

Microsoft Visual Basic 2005

CHAPTER 4

Variables andArithmetic Operations

4 Agenda

â–șLast Class: Visual Basic 3: Program Design and Coding

â–șVisual Basic 4: Variables and Arithmetic Operations

â–șAgenda for Next Class

Chapter 4: Variables and Arithmetic Operations 2

4 Last Class

â–șVisual Basic 3: Program Design and Coding‱ Most languages including VB have different

variable types such as String, Char, Numeric, and so on to represent unique aspects of a particular variable

‱ Comments stand out from the rest of the code due to its green text

‱ The scope of a variable refers to where in the program the variable can be referenced from

‱ The focus in a Windows application refers to the current object the user is interacting with

Chapter 4: Variables and Arithmetic Operations 3

4

Chapter 4: Variables and Arithmetic Operations 4

Objectives

â–șCreate, modify, and program a TextBox objectâ–șUse code to place data in the Text property of a

Label objectâ–șUse the AcceptButton and CancelButton

propertiesâ–șUnderstand and declare String and Numeric

variablesâ–șUse assignments statements to place data in

variables

4

Chapter 4: Variables and Arithmetic Operations 5

Objectives

â–șUse literals and constants in coding statementsâ–șUnderstand scope rules for variablesâ–șConvert string and numeric dataâ–șUnderstand and use arithmetic operators and

arithmetic operations

4

Chapter 4: Variables and Arithmetic Operations 6

Objectives

â–șFormat and display numeric data as a stringâ–șCreate a form load event â–șCreate a concatenated stringâ–șDebug a program

4

Chapter 4: Variables and Arithmetic Operations 7

Introduction

4

Chapter 4: Variables and Arithmetic Operations 8

TextBox Objects

â–șWith Visual Studio open and the Form1.vb [Design] tabbed page visible, point to the TextBox .NET component in the Toolbox

â–șDrag the TextBox .NET component onto the Windows Form object at the desired location

â–șWhen the upper-left corner of the pointer is located where you want the TextBox object’s upper-left corner, release the left mouse button

4

Chapter 4: Variables and Arithmetic Operations 9

TextBox Objects

4

Chapter 4: Variables and Arithmetic Operations 10

Sizing and Positioning a TextBox Object

â–șSelect the TextBox object. Scroll in the Properties window until the Text property is visible and then click the right column for the Text property

â–șType the maximum number of characters the user normally will enter into the text box and then press the ENTER key. When entering numbers, the digit 8 often is entered because it is wider than other digits. In this example, the value 888 is entered because three digits is the maximum number of digits the user normally will enter

4

Chapter 4: Variables and Arithmetic Operations 11

Sizing and Positioning a TextBox Object

â–șUsing the Font property in the Properties window, change the Font property to the correct font and font size. For this application, change the font to Times New Roman and change the font size to 12. Then, drag the right edge of the TextBox object to resize the TextBox object so it is slightly wider than the 888 entry

â–șTo horizontally align the text in the label and the text in the text box, drag the text box up until a red snap line indicates the bottoms of the text are aligned. Then, release the left mouse button

4

Chapter 4: Variables and Arithmetic Operations 12

Sizing and Positioning a TextBox Object

4

Chapter 4: Variables and Arithmetic Operations 13

Aligning Text in a TextBox Object

â–șSelect the TextBox object. In the Properties window, scroll until the TextAlign property is visible, click the TextAlign property in the left column, and then click the list arrow in the right column of the TextAlign property

â–șClick Center in the TextAlign property listâ–șBecause the TextBox object is sized properly,

remove the digits in the TextBox object. Select the characters 888 in the Text property, press the DELETE key on your keyboard, and then press the ENTER key

4

Chapter 4: Variables and Arithmetic Operations 14

Aligning Text in a TextBox Object

4

Chapter 4: Variables and Arithmetic Operations 15

Creating a MultiLine Text Box

â–șSelect the TextBox object, click the Action tag, and point to the MultiLine check box

â–șClick the MultiLine check box

4

Chapter 4: Variables and Arithmetic Operations 16

Creating a MaskedTextBox Object

4

Chapter 4: Variables and Arithmetic Operations 17

Creating a MaskedTextBox Object

â–șDrag a MaskedTextBox .NET component from the Toolbox to the Windows Form object. Then, click the Action tag on the TextBox object and point to the Set Mask command

â–șClick Set Mask on the MaskedTextBox Tasks list and then click the Short date mask description in the Input Mask dialog box

â–șClick the OK button in the Input Mask dialog box and then click anywhere in the Windows Form object

4

Chapter 4: Variables and Arithmetic Operations 18

Creating a MaskedTextBox Object

4

Chapter 4: Variables and Arithmetic Operations 19

Label Objects

â–șDrag a Label object onto the Windows Form object to the correct location. Name the label lblTotalCostOfDownloads. Change the label to the appropriate font size (Times New Roman, 12 point). In the Text property for the Label object, enter the maximum number of characters ($888.88) that will appear in the label during execution of the program

â–șDrag the Label object up until the red snap line appears. Then release the left mouse button

4

Chapter 4: Variables and Arithmetic Operations 20

Label Objects

4

Chapter 4: Variables and Arithmetic Operations 21

Accept Button in Form Properties

â–șThe program will carry out the event handler processing associated with the Accept Button if the user clicks the button or if the user presses the ENTER key

â–șClick a blank area in the Windows Form object to select it. Scroll in the Properties window until the AcceptButton property is visible. Click the AcceptButton property name in the left column and then click the AcceptButton property list arrow in the right column

â–șClick btnCalculateCost in the AcceptButton property list

4

Chapter 4: Variables and Arithmetic Operations 22

Cancel Button in Form Properties

â–șWhen the user presses the ESC key, the event handler processing for the button identified as the Cancel button will be executed

â–șClick a blank area in the Windows Form object to select it

â–șClick the CancelButton property name in the left column in the Properties window for the Windows Form object, and then click the CancelButton list arrow

â–șClick the button name (btnClear) in the CancelButton property list

4

Chapter 4: Variables and Arithmetic Operations 23

Visual Studio Preparation for Code Entry

â–șWith the Toolbox visible, click the Toolbox Close button. The Toolbox closes and the work area expands in size. To reshow the Toolbox after it has been closed, click the Toolbox button on the Standard toolbar

4

Chapter 4: Variables and Arithmetic Operations 24

String Data Type

â–șA String data type allows every character available on the computer to be stored in it

â–șThe data type for the value the user enters in a TextBox object and that is stored in the Text property of the TextBox object is string

â–șA variable is a named location in RAM where data is stored

â–șA String variable is a named location in RAM that can store a string value

4

Chapter 4: Variables and Arithmetic Operations 25

String Data Type

4

Chapter 4: Variables and Arithmetic Operations 26

Assignment Statements

â–șOne method to place data in the variable is to use an assignment statement

4

Chapter 4: Variables and Arithmetic Operations 27

Assignment Statements

â–șWith Visual Studio displaying the code editing window and the insertion point located in the desired column, type Dim followed by a space. Then, type the name of the String variable you want to define, strNumberOfSongs on your keyboard

â–șPress the SPACEBAR, type the word As and then press the SPACEBAR again

â–șBecause the entry should be String, type str on your keyboard

â–șPress the ENTER key

4

Chapter 4: Variables and Arithmetic Operations 28

Assignment Statements

â–șTo begin the assignment statement, press CTRL+SPACEBAR in order to display the IntelliSense list of allowable entries. Then, type strn to highlight the strNumberOfSongs variable name in the IntelliSense list

â–șPress the SPACEBAR, press the EQUAL SIGN key, and then press the SPACEBAR

â–șType me. to display the IntelliSense list, and then type txt to identify the txtNumberOfSongs TextBox object in the IntelliSense list

â–șPress the PERIOD key and then, if necessary, type te to highlight the Text entry in the IntelliSense list

â–șPress the ENTER key

4

Chapter 4: Variables and Arithmetic Operations 29

Assignment Statements

4

Chapter 4: Variables and Arithmetic Operations 30

Numeric Data Types

â–șA numeric data type must be used in arithmetic operations

4

Chapter 4: Variables and Arithmetic Operations 31

Numeric Data Types

4

Chapter 4: Variables and Arithmetic Operations 32

Other Data Types

4

Chapter 4: Variables and Arithmetic Operations 33

Other Data Types

4

Chapter 4: Variables and Arithmetic Operations 34

Miscellaneous Data Types

4

Chapter 4: Variables and Arithmetic Operations 35

Variable Prefixes

4

Chapter 4: Variables and Arithmetic Operations 36

Literals

â–șA value is called a literal when the value being used in the assignment statement is literally the value that is required

4

Chapter 4: Variables and Arithmetic Operations 37

Forced Literal Types

â–șSometimes you might want a literal to be a different data type than the Visual Basic default

4

Chapter 4: Variables and Arithmetic Operations 38

Constants

â–șA constant variable will contain one permanent value throughout the execution of the program

4

Chapter 4: Variables and Arithmetic Operations 39

Constants

â–șThe declaration of a constant variable begins with the letters Const, not the letters Dim

â–șYou must assign the value to be contained in the constant on the same line as the definition of the constant.

â–șYou cannot attempt to change the value in the constant variable anywhere in the program. If you attempt this, you will produce a compiler error

â–șThe letter c often is placed before the prefix of the constant variable name to identify throughout the program that it is a constant variable and cannot be changed

â–șOther than the letter c constant variable names are formed using the same rules and techniques as nonconstant names

4

Chapter 4: Variables and Arithmetic Operations 40

Referencing a Variable

â–șWhen a variable is declared, it will be underlined with a green squiggly line until it is referenced in a statement

â–șIt is mandatory when using a variable in a program that the variable is defined prior to using the variable name in a statement

4

Chapter 4: Variables and Arithmetic Operations 41

Scope of Variables

â–șThe scope of a variable specifies where within the program the variable can be referenced in a Visual Basic statement

â–șA variable can be referenced only within the region of the program where it is defined

â–șThe code between the Sub statement and the End Sub statement is a procedure

â–șA variable that can only be referenced within the region of the program where it is defined is called a local variable‱ Local variables have a certain lifetime in the program

â–șGlobal variables can be used in multiple regions of a program

4

Chapter 4: Variables and Arithmetic Operations 42

Converting Variable Data

â–șVisual Basic includes several procedures that allow you to convert one data type to another data type

â–șA procedure to convert a String data type to an Integer data type is named ToInt32

â–șThe procedure is found in the Convert class, which is available in a Visual Studio 2005 class library

4

Chapter 4: Variables and Arithmetic Operations 43

Using a Procedure

â–șA procedure that performs its task but does not return a value is called a Sub procedure

â–șA procedure that returns a value is called a Function procedure, or a function

â–șAn argument identifies a value required by a procedure

â–șEvery procedure is part of a class

4

Chapter 4: Variables and Arithmetic Operations 44

Using a Procedure

4

Chapter 4: Variables and Arithmetic Operations 45

Option Strict On

â–șVisual Basic will, by default, automatically convert data types if the data type on the right side of the equal sign in an assignment statement is different from the data type on the left side of the equal sign

â–șTo prevent automatic conversion of values, the developer must insert the Option Strict On statement in the program prior to any event handler code in the program

â–șThe Option Strict On statement explicitly disallows any default data type conversions in which data loss would occur and any conversion between numeric types and strings

4

Chapter 4: Variables and Arithmetic Operations 46

Option Strict On

4

Chapter 4: Variables and Arithmetic Operations 47

Arithmetic Operations

4

Chapter 4: Variables and Arithmetic Operations 48

Arithmetic Operators

4

Chapter 4: Variables and Arithmetic Operations 49

Multiple Operations

â–șA single assignment statement can contain multiple arithmetic operations

â–șHierarchy of Operations‱ Exponentiation (^) is performed first‱ Multiplication (*) and division (/) are performed next‱ Integer division (\) is next‱ MOD then occurs‱ Addition (1) and subtraction (2) are performed last‱ Within these five steps, calculations are performed left

to right

4

Chapter 4: Variables and Arithmetic Operations 50

Displaying Numeric Output Data

4

Chapter 4: Variables and Arithmetic Operations 51

Format Specifications for the ToString Function

â–șUse the format specifier to identify the format for the numeric data to be returned by the ToString function

4

Chapter 4: Variables and Arithmetic Operations 52

Precision Specifier

â–șThe precision specifier is a number that is included within the quotation marks in the function call to identify the number of positions to the right of the decimal point that should be returned

4

Chapter 4: Variables and Arithmetic Operations 53

Clear Procedure

â–șThe Clear procedure clears any data currently placed in the Text property of a TextBox object

4

Chapter 4: Variables and Arithmetic Operations 54

Clearing the Text Property of a Label

â–șThe Clear procedure cannot be used with a Label object

â–șYou must write an assignment statement that assigns a null length string to the Text property of a Label object

4

Chapter 4: Variables and Arithmetic Operations 55

Setting the Focus

â–șWhen the focus is on a TextBox object, the insertion point is located in the text box

4

Chapter 4: Variables and Arithmetic Operations 56

Form Load Event

â–șA form load event occurs when the program starts and the Digital Downloads form is loaded

â–șThis event handler completes the following tasks:‱ Display the cost per download heading‱ Clear the placeholder from the

lblTotalCostOfDownloads Text property‱ Set the focus on the txtNumberOfSongs text

box

4

Chapter 4: Variables and Arithmetic Operations 57

Concatenation

â–șThe process of joining two different values into a single string is called concatenation

â–șThe values being concatenated must be String data types

4

Chapter 4: Variables and Arithmetic Operations 58

Concatenation

4

Chapter 4: Variables and Arithmetic Operations 59

Class Scope

â–șWhen a variable is referenced in two different event handling procedures, it must be defined at the class level instead of the procedure (event handler) level

4

Chapter 4: Variables and Arithmetic Operations 60

Debugging Your Program

â–șA Format Exception occurs when the user enters data that a statement within the program cannot process properly

4

Chapter 4: Variables and Arithmetic Operations 61

Debugging Your Program

4

Chapter 4: Variables and Arithmetic Operations 62

Debugging Your Program

â–șAn Overflow Exception occurs when the user enters a value greater than the maximum value that can be processed by the statement

â–șIt is not possible to divide by zero, so if your program contains a division operation and the divisor is equal to zero, the Divide By Zero Exception will occur

4

Chapter 4: Variables and Arithmetic Operations 63

Program Design

4

Chapter 4: Variables and Arithmetic Operations 64

Program Design

4

Chapter 4: Variables and Arithmetic Operations 65

Event Planning Document

4

Chapter 4: Variables and Arithmetic Operations 66

Summary

â–șCreate, modify, and program a TextBox objectâ–șUse code to place data in the Text property of a

Label objectâ–șUse the AcceptButton and CancelButton

propertiesâ–șUnderstand and declare String and Numeric

variablesâ–șUse assignments statements to place data in

variables

4

Chapter 4: Variables and Arithmetic Operations 67

Summary

â–șUse literals and constants in coding statementsâ–șUnderstand scope rules for variablesâ–șConvert string and numeric dataâ–șUnderstand and use arithmetic operators and

arithmetic operations

4

Chapter 4: Variables and Arithmetic Operations 68

Summary

â–șFormat and display numeric data as a stringâ–șCreate a form load event â–șCreate a concatenated stringâ–șDebug a program

4 Next Class

â–șChapter 6: Ethics and Intellectual Property Rights

Chapter 4: Variables and Arithmetic Operations 69

4 Questions