Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.

Similar presentations


Presentation on theme: "Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations."— Presentation transcript:

1 Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations

2 4 Chapter 4: Variables and Arithmetic Operations2 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

3 4 Chapter 4: Variables and Arithmetic Operations3 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 4 Chapter 4: Variables and Arithmetic Operations4 Objectives ►Format and display numeric data as a string ►Create a form load event ►Create a concatenated string ►Debug a program

5 4 Introduction Chapter 4: Variables and Arithmetic Operations5

6 4 6 TextBox Objects ►With Visual Studio open, the Digital Downloads Windows application created, and the frmDownloads.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

7 4 TextBox Objects Chapter 4: Variables and Arithmetic Operations7

8 4 8 Sizing and Positioning a TextBox Object ►Select the TextBox object. Select the (Name) property and name the TextBox object txtNumberOfDownloads. Scroll in the Properties window until the Text property is visible and then tap or 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. Programmers often use the digit 8 in this situation because it is wider than other digits. This example uses the value 888 because three digits is the maximum number of digits the user normally will enter

9 4 Chapter 4: Variables and Arithmetic Operations9 Sizing and Positioning a TextBox Object ►Using the Properties window, change the Font property to the correct font and font size. For this application, change the font to Century Gothic and change the font size to 11. Then, drag the right edge of the TextBox object and resize it to be 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

10 4 Chapter 4: Variables and Arithmetic Operations10 Sizing and Positioning a TextBox Object

11 4 Chapter 4: Variables and Arithmetic Operations11 Aligning Text in a TextBox Object ►Select the TextBox object. In the Properties window, scroll until the TextAlign property is visible, tap or click the TextAlign property in the left column, and then tap or click the list arrow in the right column of the TextAlign property ►Tap or 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

12 4 Chapter 4: Variables and Arithmetic Operations12 Aligning Text in a TextBox Object

13 4 Chapter 4: Variables and Arithmetic Operations13 Creating a MultiLine Text Box ►Select the TextBox object, tap or click the Action tag, and then point to the MultiLine check box ►Tap or click the MultiLine check box

14 4 Chapter 4: Variables and Arithmetic Operations14 Creating a MaskedTextBox Object

15 4 Chapter 4: Variables and Arithmetic Operations15 Creating a MaskedTextBox Object ►Drag a MaskedTextBox.NET component from the Toolbox to the Windows Form object. Then, tap or click the Action tag on the TextBox object and point to the Set Mask command ►Tap or click Set Mask on the MaskedTextBox Tasks list and then tap or click the Short date mask description in the Input Mask dialog box ►Tap or click the OK button in the Input Mask dialog box and then tap or click anywhere in the Windows Form object

16 4 Chapter 4: Variables and Arithmetic Operations16 Creating a MaskedTextBox Object

17 4 Chapter 4: Variables and Arithmetic Operations17 Label Objects ►Drag a Label object onto the Windows Form object to the correct location. Name the label lblTotalCost. Change the label to the appropriate font size (Century Gothic, 11 points). 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

18 4 Chapter 4: Variables and Arithmetic Operations18 Label Objects

19 4 Chapter 4: Variables and Arithmetic Operations19 Accept Button in Form Properties ►After the Button objects are added, tap or click a blank area in the Windows Form object to select it. Scroll in the Properties window until the AcceptButton property is visible. Tap or click the AcceptButton property name in the left column and then tap or click the AcceptButton property list arrow in the right column ►Tap or click btnCalculate in the AcceptButton property list

20 4 Accept Button in Form Properties Chapter 4: Variables and Arithmetic Operations20

21 4 Chapter 4: Variables and Arithmetic Operations21 Cancel Button in Form Properties ►When the user presses the ESC key, the event handler processing will be executed for the button identified as the Cancel button ►Tap or click a blank area in the Windows Form object to select it ►Tap or click the CancelButton property name in the left column in the Properties window for the Windows Form object, and then tap or click the CancelButton list arrow ►Tap or click the button name (btnClear) in the CancelButton property list

22 4 Chapter 4: Variables and Arithmetic Operations22 Visual Studio Preparation for Code Entry ►With the Toolbox visible, tap or click the Toolbox Close button. The Toolbox closes and the work area expands in size. To display the Toolbox after it has been closed, tap or click the VIEW menu and then tap or click Toolbox

23 4 Chapter 4: Variables and Arithmetic Operations23 String Data Type ►A String data type can store any character available on the computer ►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

24 4 Chapter 4: Variables and Arithmetic Operations24 String Data Type

25 4 Chapter 4: Variables and Arithmetic Operations25 Assignment Statements ►One method to place data in the variable is to use an assignment statement

26 4 Chapter 4: Variables and Arithmetic Operations26 Assignment Statements ►With Visual Studio open to the code 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 ►Press the SPACEBAR, type the word As and then press the SPACEBAR again ►Because the entry should be String, type str ►Press the ENTER key

27 4 Chapter 4: Variables and Arithmetic Operations27 Assignment Statements ►To begin the assignment statement, type strn. IntelliSense displays the only variable name that starts with the letter strn, the String variable strNumberOfSongs ►Press the SPACEBAR, press the EQUAL SIGN key, and then press the SPACEBAR ►Type txt to display the IntelliSense list of the Form objects, and then type n to identify the txtNumberof Downloads Text Box 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

28 4 Chapter 4: Variables and Arithmetic Operations28 Assignment Statements

29 4 Chapter 4: Variables and Arithmetic Operations29 Numeric Data Types ►A numeric data type must be used in arithmetic operations

30 4 Numeric Data Types Chapter 4: Variables and Arithmetic Operations30

31 4 Chapter 4: Variables and Arithmetic Operations31 Other Data Types

32 4 Chapter 4: Variables and Arithmetic Operations32

33 4 Miscellaneous Data Types Chapter 4: Variables and Arithmetic Operations33

34 4 Variable Prefixes Chapter 4: Variables and Arithmetic Operations34

35 4 Chapter 4: Variables and Arithmetic Operations35 Literals ►A value is called a literal when it literally is the value required by the assignment statement

36 4 Chapter 4: Variables and Arithmetic Operations36 Forced Literal Types ►Sometimes you might want a literal to be a different data type than the Visual Basic default type

37 4 Chapter 4: Variables and Arithmetic Operations37 Constants ►A constant variable will contain one permanent value throughout the execution of the program

38 4 Chapter 4: Variables and Arithmetic Operations38 Constants ►The declaration of a constant 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 its definition ►You cannot attempt to change the value of the constant anywhere in the program. If you do, you will produce a compiler error ►The letter c often is placed before the constant name to identify it throughout the program as a variable that cannot be changed ►Other than the letter c, constant names are formed using the same rules and techniques as other variable names

39 4 Chapter 4: Variables and Arithmetic Operations39 Referencing a Variable ►When a variable is declared, it will be underlined with a green squiggly line until it is referenced in a statement ►When using a variable in a program, it is mandatory that you define the variable before using the variable name in a statement

40 4 Chapter 4: Variables and Arithmetic Operations40 Scope of Variables ►The scope of a variable specifies where within the program the variable can be referenced in a Visual Basic statement within the program ►You declare a variable in the region within a program ►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

41 4 Chapter 4: Variables and Arithmetic Operations41 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 2012 class library

42 4 Chapter 4: Variables and Arithmetic Operations42 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

43 4 Using a Procedure Chapter 4: Variables and Arithmetic Operations43

44 4 Chapter 4: Variables and Arithmetic Operations44 Option Strict On ►By default, Visual Basic will automatically convert data types if the one 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 ►The Option Strict On statement explicitly prevents any default data type conversions that would cause data loss and prevents any conversion between numeric types and strings

45 4 Option Strict On Chapter 4: Variables and Arithmetic Operations45

46 4 Arithmetic Operations Chapter 4: Variables and Arithmetic Operations46

47 4 Arithmetic Operators Chapter 4: Variables and Arithmetic Operations47

48 4 Chapter 4: Variables and Arithmetic Operations48 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 (+) and subtraction (-) are performed last Within these five steps, calculations are performed left to right

49 4 Displaying Numeric Output Data Chapter 4: Variables and Arithmetic Operations49

50 4 Chapter 4: Variables and Arithmetic Operations50 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

51 4 Chapter 4: Variables and Arithmetic Operations51 Precision Specifier ►The precision specifier is a number included within the quotation marks in the function call that identifies the number of positions that should be returned to the right of the decimal point

52 4 Chapter 4: Variables and Arithmetic Operations52 Clear Procedure ►The Clear procedure clears any data currently placed in the Text property of a TextBox object

53 4 Chapter 4: Variables and Arithmetic Operations53 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

54 4 Chapter 4: Variables and Arithmetic Operations54 Setting the Focus ►When the focus is on a TextBox object, the insertion point is located in the text box

55 4 Chapter 4: Variables and Arithmetic Operations55 Form Load Event ►A form load event occurs when the program starts and Windows Form Object is loaded ►This event handler completes the following tasks: Display the cost per download heading Clear the placeholder from the lblTotalCostOfCost Text property Set the focus on the txtNumberOfDownloads text box

56 4 Chapter 4: Variables and Arithmetic Operations56 Concatenation ►The process of joining two different values into a single string is called concatenation ►The values being concatenated must be String data types

57 4 Concatenation Chapter 4: Variables and Arithmetic Operations57

58 4 Chapter 4: Variables and Arithmetic Operations58 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

59 4 Debugging Your Program ►A Format Exception occurs when the user enters data that a statement within the program cannot process properly Chapter 4: Variables and Arithmetic Operations59

60 4 Chapter 4: Variables and Arithmetic Operations60 Debugging Your Program ►An Overflow Exception occurs when the user enters a value greater than the maximum value that the statement can process ►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

61 4 Program Design Chapter 4: Variables and Arithmetic Operations61

62 4 Program Design Chapter 4: Variables and Arithmetic Operations62

63 4 Event Planning Document Chapter 4: Variables and Arithmetic Operations63

64 4 Chapter 4: Variables and Arithmetic Operations64 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

65 4 Chapter 4: Variables and Arithmetic Operations65 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

66 4 Chapter 4: Variables and Arithmetic Operations66 Summary ►Format and display numeric data as a string ►Create a form load event ►Create a concatenated string ►Debug a program

67 Microsoft Visual Basic 2012 CHAPTER FOUR COMPLETE Variables and Arithmetic Operations


Download ppt "Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations."

Similar presentations


Ads by Google