Microsoft Visual Basic 2005: Reloaded Second Edition - PowerPoint PPT Presentation

Loading...

PPT – Microsoft Visual Basic 2005: Reloaded Second Edition PowerPoint presentation | free to view - id: c696-Mjg3Z



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 2005: Reloaded Second Edition

Description:

Microsoft Visual Basic 2005: Reloaded. Second Edition. Chapter 3 ... Microsoft Visual Basic 2005: Reloaded, Second Edition. 6. Selecting a Data Type for a Variable ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 75
Provided by: lasa9
Category:

less

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

Title: Microsoft Visual Basic 2005: Reloaded Second Edition


1
Microsoft Visual Basic 2005 Reloaded Second
Edition
  • Chapter 3
  • Variables, Constants, Methods, and Calculations

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 Explicit,
    Option Strict, and Imports 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

4
Objectives (continued)
  • 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

5
Variables
  • Variables computer memory locations used to
    store data while an application is running
  • Every variable has a
  • Name
  • Data type
  • Scope
  • Lifetime

6
Selecting a Data Type for a Variable
  • 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
Selecting a Data Type for a Variable (continued)
8
Selecting a Name for a Variable
  • Identifier descriptive name given to a variable
  • Use a meaningful name that reflects the purpose
    of the variable
  • Use camel casing for variable identifiers
  • Variable names must conform to naming rules

9
Selecting a Name for a Variable (continued)
10
Declaring a Variable
  • Declaration statement used to declare, or
    create, a variable
  • Declaration statement includes
  • Scope keyword Dim or Private or Static
  • Name of the variable
  • Data type
  • Initial value (optional)

11
Declaring a Variable (continued)
12
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

13
Assigning Data to an Existing Variable (continued)
14
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 with a decimal place is treated
    as a Double type
  • Literal type character forces a literal constant
    to assume a specific data type

15
Assigning Data to an Existing Variable (continued)
16
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
  • TryParse method has 4 arguments
  • String string value to be converted
  • Variable location to store the result
  • IFormatProvider (optional) specifies formatting
  • NumberStyles (optional) allows formatting
    characters to be in the data to be converted

17
Using the TryParse Method (continued)
  • IFormatProvider argument formats numbers, dates,
    and times
  • NumberFormatInfo.CurrentInfo value
  • Uses the formatting characters specified in the
    Windows Customize Regional Options dialog box

18
Using the TryParse Method (continued)
19
Using the TryParse Method (continued)
20
Using the TryParse Method (continued)
  • Assign the TryParse methods return value to a
    Boolean variable
  • If True, the conversion was successful
  • If False, the value could not be converted
  • Line continuation character the underscore (_)
  • Breaks up a long instruction into two or more
    lines
  • Must appear at end of line, preceded by a space
  • Must have an Imports statement in the General
    Declarations section of code to use NumberStyles
    and NumberformatInfo.CurrentInfo
  • Imports System.Globalization

21
Using the TryParse Method (continued)
22
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

23
Using the Convert Class (continued)
24
Writing Arithmetic Expressions
  • 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

25
Writing Arithmetic Expressions (continued)
26
Writing Arithmetic Expressions (continued)
27
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 are usually declared in two places
  • Within a procedure
  • In the forms Declarations section
  • Procedure-level variable declared within a
    procedure
  • Procedure scope only the procedure can use the
    variable

28
The Scope and Lifetime of a Variable (continued)
29
The Scope and Lifetime of a Variable (continued)
  • 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

30
The Scope and Lifetime of a Variable (continued)
31
The Scope and Lifetime of a Variable (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

32
The Scope and Lifetime of a Variable (continued)
33
The Scope and Lifetime of a Variable (continued)
  • Block scope variable can be used within a
    specific block of code
  • Block-level variable declared within a specific
    block of code

34
Static Variables
  • Static variable
  • Procedure-level variable that retains its value
    even after the procedure ends
  • Retains its value until the application ends
  • Can be used instead of a module-level variable
  • A static variable has
  • Same lifetime as a module-level variable
  • Narrower scope than a module-level variable
  • Declared using the Static keyword

35
Static Variables (continued)
36
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

37
Named Constants (continued)
38
Named Constants (continued)
39
Named Constants (continued)
40
Option Explicit and Option Strict
  • Option Explicit
  • When on, all variables used must first be
    declared
  • Protects against misspelled variable names in
    code
  • Placed in the General Declarations section of
    code editor
  • Implicit type conversion can occur if the value
    on the right side of an assignment statement is
    not the same data type as the variable on the
    left side

41
Option Explicit and Option Strict (continued)
  • Promoting when a value is converted to another
    data type that stores larger numbers
  • Demoting when a value is converted to another
    data type that stores only smaller numbers
  • Data loss can occur with demoting
  • Option Strict
  • Can be used to enforce correct data typing
  • Placed in the General Declarations section of the
    code editor

42
Option Explicit and Option Strict (continued)
  • Option Strict On follows these conversion rules
  • Strings are not implicitly converted to numbers
    or vice versa
  • Narrower data types are implicitly promoted to
    wider data types
  • Wider data types are not implicitly demoted to
    narrower data types

43
Option Explicit and Option Strict (continued)
44
Coding the Skate-Away Sales Application
45
Coding the Skate-Away Sales Application
(continued)
46
Using Pseudocode to Plan a Procedure
  • Pseudocode short phrases to describe the steps a
    procedure needs to take to accomplish its goal

47
Using Pseudocode to Plan a Procedure (continued)
48
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

49
Using a Flowchart to Plan a Procedure (continued)
50
Using a Flowchart to Plan a Procedure (continued)
51
Coding the clearButtons Click Event Procedure
52
Clearing the Contents of a Controls Text Property
53
Clearing the Contents of a Controls Text
Property (continued)
  • Zero-length string (or empty string)
  • Removes the contents in the Text property of a
    control
  • Use empty set of quotation marks
  • String.Empty used to assign an empty string to a
    controls Text property
  • For TextBox control, use the Clear method

54
Setting the Focus
  • Focus method moves the focus to a specified
    control at runtime

55
Setting the Focus (continued)
56
Setting the Focus (continued)
57
Coding the calcButtons Click Event Procedure
58
Coding the calcButtons Click Event Procedure
(continued)
59
Coding the calcButtons Click Event Procedure
(continued)
60
Testing and Debugging the Application
  • Valid data data that the application is
    expecting
  • Invalid data data that is unexpected
  • Debugging locating errors in a program
  • Syntax errors usually caused by mistyping
  • Logic errors occur when you enter an instruction
    that does not give the expected results
  • Test a program with both valid and invalid data

61
Testing and Debugging the Application (continued)
62
Testing and Debugging the Application (continued)
63
Formatting Numeric Output
  • Formatting specifying the number of decimal
    places and any special characters to display
  • 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

64
Formatting Numeric Output (continued)
65
Formatting Numeric Output (continued)
66
Formatting Numeric Output (continued)
67
Formatting Numeric Output (continued)
68
Formatting Numeric Output (continued)
69
Programming Tutorial
70
Programming Example
71
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,
    scope, and lifetime
  • Use Dim to declare a variable at block or
    procedure level
  • Use Private to declare a variable at module level

72
Summary (continued)
  • Assignment statement is used to assign values to
    an existing variable
  • Literals are constant items of data
  • Use the TryParse method to convert a string to a
    number
  • Use the Imports statement to import a namespace
  • The Convert class contains methods to convert
    values to a specified data type

73
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
  • A block-level variable is usable only within the
    block in which it is declared
  • A static variable is a procedure-level variable
    that retains its value when the procedure ends
  • Option Explicit On forces declaration of all
    variables before use

74
Summary (continued)
  • Option Strict On disallows any implicit type
    conversions that may cause a loss of data
  • Pseudocode or a flowchart is used to plan a
    procedures code
  • Use the Clear method or empty string to clear a
    textbox
  • The Focus method moves the focus to a control
  • Test a program with both valid and invalid data
About PowerShow.com