Chapter 04

2,768 views

Published on

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,768
On SlideShare
0
From Embeds
0
Number of Embeds
187
Actions
Shares
0
Downloads
190
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Chapter 04

  1. 1. CHAPTER FOUR<br />Variables andArithmetic Operations<br />
  2. 2. Chapter 4: Variables and Arithmetic Operations<br />2<br />Objectives<br />Create, modify, and program a TextBox object<br />Use code to place data in the Text property of a Label object<br />Use the AcceptButton and CancelButton properties<br />Understand and declare String and Numeric variables<br />Use assignments statements to place data in variables<br />
  3. 3. Chapter 4: Variables and Arithmetic Operations<br />3<br />Objectives<br />Use literals and constants in coding statements<br />Understand scope rules for variables<br />Convert string and numeric data<br />Understand and use arithmetic operators and arithmetic operations<br />
  4. 4. Chapter 4: Variables and Arithmetic Operations<br />4<br />Objectives<br />Format and display numeric data as a string<br />Create a form load event <br />Create a concatenated string<br />Debug a program<br />
  5. 5. Introduction<br />Chapter 4: Variables and Arithmetic Operations<br />5<br />
  6. 6. Chapter 4: Variables and Arithmetic Operations<br />6<br />TextBox Objects<br />With Visual Studio open and the frmDigitalDownloads.vb [Design] tabbed page visible, point to the TextBox .NET component in the Toolbox<br />Drag the TextBox .NET component onto the Windows Form object at the desired location<br />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<br />
  7. 7. TextBox Objects<br />Chapter 4: Variables and Arithmetic Operations<br />7<br />
  8. 8. Chapter 4: Variables and Arithmetic Operations<br />8<br />Sizing and Positioning a TextBox Object<br />Select the TextBox object. Select the (Name) property and name the TextBoxobject txtNumberOfDownloads. Scroll in the Properties window until the Text property is visible and then click the right column for the Text property<br />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<br />
  9. 9. Chapter 4: Variables and Arithmetic Operations<br />9<br />Sizing and Positioning a TextBox Object<br />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 Century Gothic 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<br />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<br />
  10. 10. Chapter 4: Variables and Arithmetic Operations<br />10<br />Sizing and Positioning a TextBox Object<br />
  11. 11. Chapter 4: Variables and Arithmetic Operations<br />11<br />Aligning Text in a TextBox Object<br />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<br />Click Center in the TextAlign property list<br />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<br />
  12. 12. Chapter 4: Variables and Arithmetic Operations<br />12<br />Aligning Text in a TextBox Object<br />
  13. 13. Chapter 4: Variables and Arithmetic Operations<br />13<br />Creating a MultiLine Text Box<br />Select the TextBox object, click the Action tag, and point to the MultiLine check box<br />Click the MultiLine check box<br />
  14. 14. Chapter 4: Variables and Arithmetic Operations<br />14<br />Creating a MaskedTextBox Object<br />
  15. 15. Chapter 4: Variables and Arithmetic Operations<br />15<br />Creating a MaskedTextBox Object<br />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<br />Click Set Mask on the MaskedTextBox Tasks list and then click the Short date mask description in the Input Mask dialog box<br />Click the OK button in the Input Mask dialog box and then click anywhere in the Windows Form object<br />
  16. 16. Chapter 4: Variables and Arithmetic Operations<br />16<br />Creating a MaskedTextBox Object<br />
  17. 17. Chapter 4: Variables and Arithmetic Operations<br />17<br />Label Objects<br />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 (Century Gothic, 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<br />Drag the Label object up until the red snap line appears. Then release the left mouse button<br />
  18. 18. Chapter 4: Variables and Arithmetic Operations<br />18<br />Label Objects<br />
  19. 19. Chapter 4: Variables and Arithmetic Operations<br />19<br />Accept Button in Form Properties<br />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<br />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<br />Click btnCalculateCost in the AcceptButton property list<br />
  20. 20. Accept Button in Form Properties<br />Chapter 4: Variables and Arithmetic Operations<br />20<br />
  21. 21. Chapter 4: Variables and Arithmetic Operations<br />21<br />Cancel Button in Form Properties<br />When the user presses the ESC key, the event handler processing for the button identified as the Cancel button will be executed<br />Click a blank area in the Windows Form object to select it<br />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<br />Click the button name (btnClear) in the CancelButton property list<br />
  22. 22. Chapter 4: Variables and Arithmetic Operations<br />22<br />Visual Studio Preparation for Code Entry<br />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<br />
  23. 23. Chapter 4: Variables and Arithmetic Operations<br />23<br />String Data Type<br />A String data type allows every character available on the computer to be stored in it<br />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<br />A variable is a named location in RAM where data is stored<br />A String variable is a named location in RAM that can store a string value<br />
  24. 24. Chapter 4: Variables and Arithmetic Operations<br />24<br />String Data Type<br />
  25. 25. Chapter 4: Variables and Arithmetic Operations<br />25<br />Assignment Statements<br />One method to place data in the variable is to use an assignment statement<br />
  26. 26. Chapter 4: Variables and Arithmetic Operations<br />26<br />Assignment Statements<br />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<br />Press the SPACEBAR, type the word As and then press the SPACEBAR again<br />Because the entry should be String, type str on your keyboard<br />Press the ENTER key<br />
  27. 27. Chapter 4: Variables and Arithmetic Operations<br />27<br />Assignment Statements<br />To begin the assignment statement, type strn. IntelliSense displays the only variable name that starts with the letter strn, the String variable strNumberOfDownloads<br />Press the SPACEBAR, press the EQUAL SIGN key, and then press the SPACEBAR<br />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<br />Press the PERIOD key and then, if necessary, type te to highlight the Text entry in the IntelliSense list<br />Press the ENTER key<br />
  28. 28. Chapter 4: Variables and Arithmetic Operations<br />28<br />Assignment Statements<br />
  29. 29. Chapter 4: Variables and Arithmetic Operations<br />29<br />Numeric Data Types<br />A numeric data type must be used in arithmetic operations<br />
  30. 30. Numeric Data Types<br />Chapter 4: Variables and Arithmetic Operations<br />30<br />
  31. 31. Chapter 4: Variables and Arithmetic Operations<br />31<br />Other Data Types<br />
  32. 32. Other Data Types<br />Chapter 4: Variables and Arithmetic Operations<br />32<br />
  33. 33. Miscellaneous Data Types<br />Chapter 4: Variables and Arithmetic Operations<br />33<br />
  34. 34. Variable Prefixes<br />Chapter 4: Variables and Arithmetic Operations<br />34<br />
  35. 35. Chapter 4: Variables and Arithmetic Operations<br />35<br />Literals<br />A value is called a literal when the value being used in the assignment statement is literally the value that is required<br />
  36. 36. Chapter 4: Variables and Arithmetic Operations<br />36<br />Forced Literal Types<br />Sometimes you might want a literal to be a different data type than the Visual Basic default<br />
  37. 37. Chapter 4: Variables and Arithmetic Operations<br />37<br />Constants<br />A constant variable will contain one permanent value throughout the execution of the program<br />
  38. 38. Chapter 4: Variables and Arithmetic Operations<br />38<br />Constants<br />The declaration of a constant variable begins with the letters Const, not the letters Dim<br />You must assign the value to be contained in the constant on the same line as the definition of the constant.<br />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<br />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<br />Other than the letter c constant variable names are formed using the same rules and techniques as nonconstant names<br />
  39. 39. Chapter 4: Variables and Arithmetic Operations<br />39<br />Referencing a Variable<br />When a variable is declared, it will be underlined with a green squiggly line until it is referenced in a statement<br />It is mandatory when using a variable in a program that the variable is defined prior to using the variable name in a statement<br />
  40. 40. Chapter 4: Variables and Arithmetic Operations<br />40<br />Scope of Variables<br />The scope of a variable specifies where within the program the variable can be referenced in a Visual Basic statement<br />A variable can be referenced only within the region of the program where it is defined<br />The code between the Sub statement and the End Sub statement is a procedure<br />A variable that can only be referenced within the region of the program where it is defined is called a local variable<br />Local variables have a certain lifetime in the program<br />Global variables can be used in multiple regions of a program<br />
  41. 41. Chapter 4: Variables and Arithmetic Operations<br />41<br />Converting Variable Data<br />Visual Basic includes several procedures that allow you to convert one data type to another data type<br />A procedure to convert a String data type to an Integer data type is named ToInt32<br />The procedure is found in the Convert class, which is available in a Visual Studio 2010 class library<br />
  42. 42. Chapter 4: Variables and Arithmetic Operations<br />42<br />Using a Procedure<br />A procedure that performs its task but does not return a value is called a Sub procedure<br />A procedure that returns a value is called a Function procedure, or a function<br />An argument identifies a value required by a procedure<br />Every procedure is part of a class<br />
  43. 43. Using a Procedure<br />Chapter 4: Variables and Arithmetic Operations<br />43<br />
  44. 44. Chapter 4: Variables and Arithmetic Operations<br />44<br />Option Strict On<br />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<br />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<br />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<br />
  45. 45. Option Strict On<br />Chapter 4: Variables and Arithmetic Operations<br />45<br />
  46. 46. Arithmetic Operations<br />Chapter 4: Variables and Arithmetic Operations<br />46<br />
  47. 47. Arithmetic Operators<br />Chapter 4: Variables and Arithmetic Operations<br />47<br />
  48. 48. Chapter 4: Variables and Arithmetic Operations<br />48<br />Multiple Operations<br />A single assignment statement can contain multiple arithmetic operations<br />Hierarchy of Operations<br />Exponentiation (^) is performed first<br />Multiplication (*) and division (/) are performed next<br />Integer division () is next<br />MOD then occurs<br />Addition (+) and subtraction (-) are performed last<br />Within these five steps, calculations are performed left to right<br />
  49. 49. Displaying Numeric Output Data<br />Chapter 4: Variables and Arithmetic Operations<br />49<br />
  50. 50. Chapter 4: Variables and Arithmetic Operations<br />50<br />Format Specifications for the ToString Function<br />Use the format specifierto identify the format for the numeric data to be returned by the ToString function<br />
  51. 51. Chapter 4: Variables and Arithmetic Operations<br />51<br />Precision Specifier<br />The precision specifieris 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<br />
  52. 52. Chapter 4: Variables and Arithmetic Operations<br />52<br />Clear Procedure<br />The Clear procedure clears any data currently placed in the Text property of a TextBox object<br />
  53. 53. Chapter 4: Variables and Arithmetic Operations<br />53<br />Clearing the Text Property of a Label<br />The Clear procedure cannot be used with a Label object<br />You must write an assignment statement that assigns a null length string to the Text property of a Label object<br />
  54. 54. Chapter 4: Variables and Arithmetic Operations<br />54<br />Setting the Focus<br />When the focus is on a TextBox object, the insertion point is located in the text box<br />
  55. 55. Chapter 4: Variables and Arithmetic Operations<br />55<br />Form Load Event<br />A form load event occurs when the program starts and the Digital Downloads form is loaded<br />This event handler completes the following tasks:<br />Display the cost per download heading<br />Clear the placeholder from the lblTotalCostOfDownloads Text property<br />Set the focus on the txtNumberOfDownloads text box<br />
  56. 56. Chapter 4: Variables and Arithmetic Operations<br />56<br />Concatenation<br />The process of joining two different values into a single string is called concatenation<br />The values being concatenated must be String data types<br />
  57. 57. Concatenation<br />Chapter 4: Variables and Arithmetic Operations<br />57<br />
  58. 58. Chapter 4: Variables and Arithmetic Operations<br />58<br />Class Scope<br />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<br />
  59. 59. Debugging Your Program<br />A Format Exception occurs when the user enters data that a statement within the program cannot process properly<br />Chapter 4: Variables and Arithmetic Operations<br />59<br />
  60. 60. Chapter 4: Variables and Arithmetic Operations<br />60<br />Debugging Your Program<br />An Overflow Exception occurs when the user enters a value greater than the maximum value that can be processed by the statement<br />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<br />
  61. 61. Program Design<br />Chapter 4: Variables and Arithmetic Operations<br />61<br />
  62. 62. Program Design<br />Chapter 4: Variables and Arithmetic Operations<br />62<br />
  63. 63. Event Planning Document<br />Chapter 4: Variables and Arithmetic Operations<br />63<br />
  64. 64. Chapter 4: Variables and Arithmetic Operations<br />64<br />Summary<br />Create, modify, and program a TextBox object<br />Use code to place data in the Text property of a Label object<br />Use the AcceptButton and CancelButton properties<br />Understand and declare String and Numeric variables<br />Use assignments statements to place data in variables<br />
  65. 65. Chapter 4: Variables and Arithmetic Operations<br />65<br />Summary<br />Use literals and constants in coding statements<br />Understand scope rules for variables<br />Convert string and numeric data<br />Understand and use arithmetic operators and arithmetic operations<br />
  66. 66. Chapter 4: Variables and Arithmetic Operations<br />66<br />Summary<br />Format and display numeric data as a string<br />Create a form load event <br />Create a concatenated string<br />Debug a program<br />
  67. 67. CHAPTER FOUR COMPLETE<br />Variables andArithmetic Operations<br />

×