microsoft visual basic 2005 chapter 4 variables and arithmetic operations
TRANSCRIPT
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 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 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 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 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 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 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 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 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 30
Numeric Data Types
âșA numeric data type must be used in arithmetic operations
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 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 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 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 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 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 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