Chapter 3 Examples The examples from chapter 3

  • Slides: 58
Download presentation
Chapter 3 – Examples The examples from chapter 3, combining the data types, variables,

Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls (list boxes, text boxes) These examples illustrate the concepts discussed earlier 1

Example 3. 1. 2 2

Example 3. 1. 2 2

Example 3. 1. 2 This is a List. Box called lst. Results 3

Example 3. 1. 2 This is a List. Box called lst. Results 3

Example 3. 1. 2 Variable declarations Variable a is uninitialized so starts with a

Example 3. 1. 2 Variable declarations Variable a is uninitialized so starts with a value of 0. Variable b is initialized to 3. 4

Example 3. 1. 2 Calling methods of the List. Box object’s Items property The

Example 3. 1. 2 Calling methods of the List. Box object’s Items property The Clear method empties the Listbox. The Add method adds a row to the List. Box. 5

Anatomy of a Method Call The List. Box When calling the Add method, you

Anatomy of a Method Call The List. Box When calling the Add method, you pass it the data as an argument. lst. Results. Items. Add(a) The Items property contains the collection of data that are displayed in the List. Box The Items property’s Add method is a subroutine that places an item into the collection 6

Example 3. 1. 2 Assigning a value into the a variable 7

Example 3. 1. 2 Assigning a value into the a variable 7

Example 3. 1. 2 Adding a third item to the list 8

Example 3. 1. 2 Adding a third item to the list 8

Method Call passing a complex expression as an argument lst. Results. Items. Add(a *

Method Call passing a complex expression as an argument lst. Results. Items. Add(a * (2 + b)) An argument can be a complex expression. The expression will be fully evaluated before the resulting data is sent. In this case, the following steps take place in this order: 1) 2 + b innermost parentheses 2) Multiply a times the result if (1) 5 25 3) Pass the result of (2) to the Add method Order of operations from innermost to outermost based on parentheses 9

Example 3. 1. 4 10

Example 3. 1. 4 10

Example 3. 1. 4 This is a List. Box called lst. Results 11

Example 3. 1. 4 This is a List. Box called lst. Results 11

Example 3. 1. 4 Variable declarations 12

Example 3. 1. 4 Variable declarations 12

Example 3. 1. 4 Variable assignments 13

Example 3. 1. 4 Variable assignments 13

Example 3. 1. 4 Empty the list box items 14

Example 3. 1. 4 Empty the list box items 14

Example 3. 1. 4 Add three values to the list box 15

Example 3. 1. 4 Add three values to the list box 15

Order of operations lst. Results. Items. Add(Math. Sqrt(5 * b + 1)) 15 16

Order of operations lst. Results. Items. Add(Math. Sqrt(5 * b + 1)) 15 16 4 Add to list 16

Order of operations lst. Results. Items. Add(Int(a ^ b + 0. 8)) 8 8

Order of operations lst. Results. Items. Add(Int(a ^ b + 0. 8)) 8 8 Add to list The Int function truncates the number…it will not round up, but rather just chops off the fractional part. 17

Order of operations lst. Results. Items. Add(Math. Round(a / b, 3)) 0. 66666… 0.

Order of operations lst. Results. Items. Add(Math. Round(a / b, 3)) 0. 66666… 0. 667 Add to list The Math. Round method rounds the number either up or down, depending on which rounded value is nearer. It can take two arguments: 1) The number to be rounded 2) The total number of decimal places for the rounded number If the second argument is not provided, 0 is assumed. There will be no decimal places, so the result will be a whole number. Multiple arguments to methods are separated by commas. 18

Example 3. 1. 5 19

Example 3. 1. 5 19

Example 3. 1. 5 This example converts 41 inches into 3 feet, 5 inches

Example 3. 1. 5 This example converts 41 inches into 3 feet, 5 inches 20

Example 3. 1. 5 Variable declarations…three in one statement 21

Example 3. 1. 5 Variable declarations…three in one statement 21

Example 3. 1. 5 Backslash is for integer division. Truncates the fractional part. So,

Example 3. 1. 5 Backslash is for integer division. Truncates the fractional part. So, feet = 3 22

Example 3. 1. 5 Mod gives the remainder of an integer division. So, inches

Example 3. 1. 5 Mod gives the remainder of an integer division. So, inches = 5. 41 divided by 12 is 3, with a remainder of 5 23

Example 3. 2. 1 This is a string literal, which will be displayed literally.

Example 3. 2. 1 This is a string literal, which will be displayed literally. This is a string variable, so its contents will be displayed 24

Example 3. 2. 2 25

Example 3. 2. 2 25

Example 3. 2. 2 This example adds the values in two textboxes and places

Example 3. 2. 2 This example adds the values in two textboxes and places them in a third 26

Example 3. 2. 2 The CDbl function takes a string value and attempts to

Example 3. 2. 2 The CDbl function takes a string value and attempts to convert it into a Double value. Note: the string must consist of digits (and perhaps one dot for a decimal point). Otherwise an exception (run-time error) would occur. 27

Example 3. 2. 2 The CStr function takes a numeric value and converts it

Example 3. 2. 2 The CStr function takes a numeric value and converts it to a string. This is necessary because a Text. Box’s Text property requires a string value. 28

3 Program Modes • Design mode • Run mode • Debug or Break mode

3 Program Modes • Design mode • Run mode • Debug or Break mode 29

Stepping through program • Execute one line of code at a time (Stepping into)

Stepping through program • Execute one line of code at a time (Stepping into) • Execute one procedure (Stepping over) • Execute remaining lines of code (Stepping out) • Execution will stop a pre-specified line of code (Break point) • Hoover to see value of particular variable or 30 object

Stepping through program • Each line is highlighted before execution • Hoover to see

Stepping through program • Each line is highlighted before execution • Hoover to see values • Num 1 & both Textboxes • num 2 is 0 – Why? 31

Break points • Set a break point & run • Program stops • Hoover

Break points • Set a break point & run • Program stops • Hoover to see values 32

Example 3. 2. 3 33

Example 3. 2. 3 33

Example 3. 2. 3 Declaring three String variables 34

Example 3. 2. 3 Declaring three String variables 34

Example 3. 2. 3 Assigning String literals into String variables. 35

Example 3. 2. 3 Assigning String literals into String variables. 35

Example 3. 2. 3 Concatenating the contents of two String variables and assigning them

Example 3. 2. 3 Concatenating the contents of two String variables and assigning them into another String variable Note: remember that the right side of an assignment statement is an expression. In this example, the expressions are String expressions. 36

Example 3. 2. 3 The final result placed in the Text property of txt.

Example 3. 2. 3 The final result placed in the Text property of txt. Output is the result of another concatenation 37

Example 3. 2. 5 This example illustrates the use of some String methods and

Example 3. 2. 5 This example illustrates the use of some String methods and properties. 38

Example 3. 2. 5 The Substring method takes two arguments: 1) The beginning position

Example 3. 2. 5 The Substring method takes two arguments: 1) The beginning position of the substring (first position of the string is 0) 2) The length of the substring (number of characters to return) 39

Example 3. 2. 5 This Index. Of method returns the first position of a

Example 3. 2. 5 This Index. Of method returns the first position of a substring within a string. 40

Example 3. 2. 5 This To. Upper method converts the characters of a string

Example 3. 2. 5 This To. Upper method converts the characters of a string to upper case. There is also a To. Lower method In this case, the string expression is a concatenation. Note that (str 1 & str 2) is a concatenation of two strings. Because this is in parentheses, the concatenation occurs BEFORE the conversion to upper case. What would happen if you did not have the parentheses around str 1 & str 2? 41

Example 3. 2. 5 The Trim method removes beginning and end spaces from a

Example 3. 2. 5 The Trim method removes beginning and end spaces from a string. Here, the trim takes place for str 1, and the result is concatenated with str 2 42

Example 3. 2. 5 The Length property of a string gives the total number

Example 3. 2. 5 The Length property of a string gives the total number of characters in the string. As you can see, str 2 contains “a wink”, which has a total of six characters In this case, the Substring method is only taking ONE argument (str 2. Length – 4) 2. If a second argument is not provided Substring returns the remainder of the string, starting at the specified beginning position So, at the end of the assignment statement, str 3 contains the string “wink”. 43

Example 3. 2. 5 Note: These methods (Sub. String, Index. Of, Trim, To. Upper)

Example 3. 2. 5 Note: These methods (Sub. String, Index. Of, Trim, To. Upper) return a string expression that can be used for display or assignment. But they do NOT change the original contents of the variables str 1 and str 2. Only an assignment changes them. So, at the end of this program, the contents of str 1 and str 2 are still “Quick as ” and 44 “a wink”.

Example 3. 2. 7 It is good programming practice to include comments in your

Example 3. 2. 7 It is good programming practice to include comments in your code in order to explain what is being done. 45

Example 3. 2. 8 In previous examples, the variables were declared INSIDE the procedure.

Example 3. 2. 8 In previous examples, the variables were declared INSIDE the procedure. Here, it is declared OUTSIDE any subroutines. Two things to note about class-level variables: 1) Their data remain in existence throughout the entire time that the Form is running 2) Their data is accessible by ALL subroutines and functions of the form 46

Example 3. 2. 8 Compare previous to what would happen if num. Times were

Example 3. 2. 8 Compare previous to what would happen if num. Times were declared INSIDE the procedure. In this case, num. Times would only exist as long as the subroutine was running, and would disappear when it ended. So, EVERY TIME you run the routine, it will reinitialize to zero. Wouldn’t be able to keep a count of how many times the button was pushed! 47

Example 3. 3. 1 Working with dates In this case, we have declared a

Example 3. 3. 1 Working with dates In this case, we have declared a variable as a Date. 48

Example 3. 3. 1 Note that we have set a mask in the mtb.

Example 3. 3. 1 Note that we have set a mask in the mtb. Day. Of. Birth Masked. Text. Box control. The Masked. Text. Box’s Mask property helps guide the user and prevent invalid input. Here we guarantee that only numbers are entered separated by slashes 49

Masked Text Box Control Click on the Tasks button to reveal the Set Mask

Masked Text Box Control Click on the Tasks button to reveal the Set Mask property. Click Set Mask to invoke the Input Mask dialog box. 50

Input Mask Dialog Box 51

Input Mask Dialog Box 51

Mask A Mask setting is a sequence of characters, with 0, L, and &

Mask A Mask setting is a sequence of characters, with 0, L, and & having special meanings. • 0 Placeholder for a digit. • L Placeholder for a letter. • & Placeholder for a character 52

Sample Masks • State abbreviation: LL • Phone number: 000 -0000 • Social Security

Sample Masks • State abbreviation: LL • Phone number: 000 -0000 • Social Security Number: 000 -00 -0000 • License plate: &&&&&& • Date: 00/00/0000 53

Example 3. 3. 1 The CDate function converts a String to a Date 54

Example 3. 3. 1 The CDate function converts a String to a Date 54

Example 3. 3. 1 The Format. Datetime takes a date and formats it four

Example 3. 3. 1 The Format. Datetime takes a date and formats it four output. Options are Long. Date, Short. Date, General. Date, Longtime, and Short. Time 55

Example 3. 3. 1 The Date. Diff function gives the time gap between two

Example 3. 3. 1 The Date. Diff function gives the time gap between two dates. You can specify which intervals you want: days, months, etc. 56

Example 3. 3. 1 Format. Number allows you to display a number in a

Example 3. 3. 1 Format. Number allows you to display a number in a wide variety of formats. In this case we are specifying to display only the whole number part of the difference. As always, use parentheses to determine the order of operations that take place…inner to outer. 57

Formatting Numbers • Lots of approaches: • Format. Number is good for getting the

Formatting Numbers • Lots of approaches: • Format. Number is good for getting the correct number of places to the right of the decimal, and has other options for how to display negative numbers, etc. • Format. Currency is a great way to show dollars and cents. For example, try this: • Format. Currency( var. Name ) • Where var. Name is the name of a numeric variable 58