Microsoft Visual Basic 2008: Reloaded Third Edition - PowerPoint PPT Presentation

Loading...

PPT – Microsoft Visual Basic 2008: Reloaded Third Edition PowerPoint presentation | free to download - id: 1da919-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Microsoft Visual Basic 2008: Reloaded Third Edition

Description:

none – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 64
Provided by: peopleMor
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Microsoft Visual Basic 2008: Reloaded Third Edition


1
Microsoft Visual Basic 2008 Reloaded Third
Edition
  • Chapter Three
  • Variables, Constants,
  • and Arithmetic Operators

2
Objectives
  • After studying this chapter, you should be able
    to
  • Declare variables and named constants
  • Assign data to an existing variable
  • Convert data to the appropriate type using the
    TryParse method and the Convert class methods
  • Write arithmetic expressions
  • Understand the scope and lifetime of variables
    and named constants

3
Objectives (continued)
  • Understand the purpose of the Option statements
  • Use a TOE chart, pseudocode, and a flowchart to
    code an application
  • Clear the contents of a controls Text property
    while an application is running
  • Send the focus to a control while the application
    is running
  • Explain the difference between syntax errors and
    logic errors
  • Format an applications numeric output

4
Variables
  • Variables computer memory locations used to
    store data while an application is running
  • Use a meaningful variable name that reflects the
    purpose of the variable
  • Use camel casing for variable identifiers
  • Variable names should conform to naming rules

5
Variables (continued)
Figure 3-1 How to name a variable
6
Variables (continued)
  • Each variable must be assigned a data type
  • Data type the type of data the variable can
    store
  • Each data type is a class
  • Unicode
  • Universal coding scheme for characters
  • Assigns a unique numeric value to each character

7
Figure 3-2 Basic data types in Visual Basic
8
Declaring a Variable in Code
  • Declaration statement used to declare, or
    create, a variable
  • Declaration statement includes
  • Scope keyword Dim, Private, or Static
  • Name of the variable
  • Data type
  • Initial value (optional)
  • Numeric data types are automatically initialized
    to 0
  • String data type is automatically initialized to

9
Figure 3-3 How to declare a variable
10
Assigning Data to an Existing Variable
  • Assignment statement
  • Used to assign values to properties of controls
  • Used to assign values to variables
  • Assignment operator ()
  • Value on the right of the operator is assigned
    to the variable on the left of the operator

11
Assigning Data to an Existing Variable (continued)
  • String group of characters enclosed in quotation
    marks
  • Literal constant
  • An item of data whose value does not change while
    the application is running
  • Can be a numeric or a string literal constant
  • A numeric literal without a decimal place is
    treated as an integer
  • A numeric literal with a decimal place is treated
    as a Double type

12
Figure 3-4 How to assign a value to a variable
13
Using the TryParse Method
  • Method a specific portion of a classs
    instructions that performs a task for the class
  • TryParse method
  • Part of every numeric data types class
  • Used to convert a string to that numeric data
    type
  • Argument a value that is provided to a method
  • Basic syntax of TryParse method has two
    arguments
  • String string value to be converted
  • Variable location to store the result

14
Using the TryParse Method (continued)
  • If TryParse conversion is successful, the method
    stores the value in the variable
  • If unsuccessful, a 0 is stored in the numeric
    variable

15
Figure 3-5 How to use the basic syntax of the
TryParse method
16
Using the Convert Class
  • Convert class
  • Contains methods for converting numeric values to
    specific data types
  • Use the dot member access operator to separate
    the class name from the method name
  • Commonly used methods of the Convert class
    include
  • ToDouble
  • ToDecimal
  • ToInt32
  • ToString

17
Figure 3-6 How to use the Convert class methods
18
Option Explicit, Option Infer, and Option Strict
  • Undeclared variable a variable that does not
    appear in a declaration statement (such as Dim)
  • Is assigned a data type of Object
  • Misspelling a variable name can result in an
    undeclared variable unless Option Explicit is on
  • Option Explicit On statement
  • Appears in the General Declarations section of
    the Code Editor window (above Public Class
    statement)
  • Enforces that all variables must be declared
    before being used

19
Option Explicit, Option Infer, and Option Strict
(continued)
  • Option Infer Off statement ensures that every
    variable is declared with a data type
  • Implicit type conversion occurs when you attempt
    to assign data of one type to a variable of
    another type without explicitly attempting to
    convert it
  • If converted to a data type that can store larger
    numbers, the value is said to be promoted
  • If converted to a data type that can store only
    smaller numbers, the value is said to be demoted
  • Can cause truncation and loss of precision

20
Option Explicit, Option Infer, and Option Strict
(continued)
  • Option Strict On statement ensures that values
    cannot be converted from one data type to a
    narrower data type, resulting in lost precision

Figure 3-8 Option statements entered in the
General Declarations section
21
Figure 3-7 Rules and examples of type conversions
22
Using a Variable in an Arithmetic Expression
  • Arithmetic operators used to perform
    calculations
  • Precedence number indicates the order in which
    an operation in an expression is performed
  • If an expression has two operators with the same
    precedence, they are evaluated from left to right
  • Use parentheses to change the order of evaluation
  • Integer division operator (\) divides two
    integers and returns an integer value
  • Modulus arithmetic operator (Mod) divides two
    numbers and returns the remainder

23
Using a Variable in an Arithmetic Expression
(continued)
Figure 3-9 Most commonly used arithmetic
operators and order of precedence
24
Figure 3-10 How to include arithmetic
expressions in assignment statements
25
Using a Variable in an Arithmetic Expression
(continued)
  • Line continuation character (_)
  • Used to break up a long instruction into two or
    more physical lines
  • Underscore must be preceded by a space and must
    appear at the end of a physical line
  • A variable can store only one value at a time
  • A second assignment statement on the same
    variable will replace its current value with the
    new value

26
The Scope and Lifetime of a Variable
  • Scope indicates where the variable can be used
  • Lifetime indicates how long the variable remains
    in memory
  • Variables can have module scope, procedure scope,
    or block scope
  • Module scope variable is declared in the forms
    Declarations section
  • Variables declared within a procedure have either
    procedure scope or block scope

27
Using Variables Having Procedure Scope
  • Procedure-level variable declared within a
    procedure
  • Use the Dim keyword in the declaration
  • Procedure scope only the procedure can use the
    variable
  • With procedure-level scope, two procedures can
    each use the same variable names
  • Comments
  • Used to internally document the procedure
  • Are ignored by the compiler
  • Appear in green in the Code Editor

28
Figure 3-11 The MainForm in the Sales Tax
application
Figure 3-12 Examples of using procedure-level
variables
29
Using a Variable Having Module Scope (continued)
  • Module scope variable can be used by all
    procedures in the form
  • Module-level variable
  • Declared in the forms Declarations section
  • Use Private keyword in declaration
  • Module-level variables retain their values until
    the application ends

30
Figure 3-13 Example of using a module-level
variable
31
Static Variables
  • Static variable
  • Procedure-level variable that retains its value
    even after the procedure ends
  • Retains its value until the application ends
    (like a module-level variable), but can only be
    used by the procedure in which it is declared
  • A static variable has
  • Same lifetime as a module-level variable
  • Narrower scope than a module-level variable
  • Declared using the Static keyword

32
Figure 3-14 The MainForm in the Total Sales
application
Figure 3-15 Example of using a static variable
33
Named Constants
  • Named constant memory location whose value
    cannot be changed while the application is
    running
  • Declared using the Const keyword
  • Good programming practice to specify the data
    type as well
  • Many programmers use Pascal case for named
    constants
  • Literal type character forces a literal constant
    to assume a specific data type
  • Named constants help to document the program code

34
Figure 3-16 How to declare a named constant
35
Figure 3-17 The MainForm in the Area Calculator
application
Figure 3-18 Example of using a named constant
36
Coding the Sunshine Cellular Application
Figure 3-19 Sunshine Cellular interface
37
Coding the Sunshine Cellular Application
(continued)
Figure 3-20 Sunshine Cellular TOE chart
38
Using Pseudocode to Plan a Procedure
  • Pseudocode short phrases that describe the steps
    a procedure needs to take to accomplish its goal

Figure 3-21 Pseudocode for the Sunshine Cellular
application
39
Using a Flowchart to Plan a Procedure
  • Flowchart uses standardized symbols to show the
    steps a procedure must take to accomplish its
    goal
  • Can be used in place of pseudocode for planning
  • Three symbols
  • Start/stop symbol (oval) indicates start and
    stop points
  • Process symbol (rectangle) represents tasks
  • Input/output symbol (parallelogram) represents
    input or output tasks
  • Flowlines connect the symbols to show the
    direction

40
Figure 3-22 Flowcharts for the Sunshine Cellular
application
41
Coding the Calculate Order Buttons Click Event
Procedure
Figure 3-23 Pseudocode for the calcButtons
Click event procedure
42
Coding the Calculate Order Buttons Click Event
Procedure (continued)
Figure 3-24 Named constants and variables for
the calcButtons Click event procedure
43
Coding the Calculate Order Buttons Click Event
Procedure (continued)
Figure 3-25 Declaration statements entered in
the calcButtons Click event procedure
44
Coding the Calculate Order Buttons Click Event
Procedure (continued)
Figure 3-26 User input assigned to variables
45
Coding the Calculate Order Buttons Click Event
Procedure (continued)
Figure 3-27 Completed calcButtons Click event
procedure
46
Completing the Sunshine Cellular Application
  • Focus method moves the focus to a specified
    control when the application is running

Figure 3-28 Pseudocode for the clearButtons
Click event procedure
47
Figure 3-29 The Sunshine Cellular applications
code
48
Figure 3-29 The Sunshine Cellular applications
code (continued)
49
Testing and Debugging the Application
  • Bug an error in the program code
  • Valid data data that the application is
    expecting
  • Invalid data data that is unexpected
  • Debugging process of locating and correcting
    errors in a program

50
Testing and Debugging the Application (continued)
  • Syntax error an error that violates the
    programming languages syntax
  • Usually caused by mistyping
  • Logic error occurs when you enter an instruction
    that does not give the expected results
  • Test a program with both valid and invalid data

51
Testing and Debugging the Application (continued)
Figure 3-30 Result of testing the application
using valid data
52
Testing and Debugging the Application (continued)
Figure 3-31 Result of testing the application
using invalid data
53
Formatting Numeric Output
  • Formatting specifying the number of decimal
    places and any special characters to display
  • ToString method of a variable can be used to
    format a number
  • Format specifier specifies the type of
    formatting to use
  • Precision specifier controls the number of
    significant digits or zeros to the right of the
    decimal point

54
Figure 3-32 How to format a number
55
Formatting Numeric Output (continued)
Figure 3-33 calcbuttons modified Click event
procedure
56
Formatting Numeric Output (continued)
Figure 3-34 Formatted output shown in the
interface
57
Programming Tutorial
Figure 3-36 User interface
58
Programming Example
Figure 3-44 Pseudocode
59
Summary
  • Variables and named constants are memory
    locations that store data
  • Variables can change value, but constants cannot
  • Variables and constants have a name, data type,
    initial value, scope, and lifetime
  • Use Dim or Static to declare a variable at block
    or procedure level
  • Use Private to declare a variable at module level

60
Summary (continued)
  • Assignment statement is used to assign values to
    an existing variable
  • Literals are constant items of data that do not
    change
  • String literal constants are enclosed in
    quotation marks
  • Use the TryParse method to convert a string to a
    number
  • The Convert class contains methods to convert
    values to a specified data type

61
Summary (continued)
  • Option Explicit On forces declaration of all
    variables before use
  • Option Infer Off warns if a variable declaration
    does not include a data type
  • Option Strict On disallows any implicit type
    conversions that may cause a loss of data
  • Integer division operator divides two integers
    and returns the result
  • Modulus operator divides two numbers and returns
    the remainder

62
Summary (continued)
  • A procedure-level variable is usable only by the
    procedure in which it is declared
  • A module-level variable is usable by all
    procedures in the form
  • Use comments to document your code
  • A static variable is a procedure-level variable
    that retains its value even when the procedure
    ends
  • Pseudocode or a flowchart is used to plan a
    procedures code

63
Summary (continued)
  • You can clear the contents of a text box or label
    control by assigning an empty string or
    String.Empty value
  • The Focus method moves the focus to a control
  • Test a program with both valid and invalid data
  • You can format a programs numeric output with
    special characters, such as for currency,
    percentages, and number of decimal places
About PowerShow.com