Microsoft VB 2005: Reloaded, Advanced - PowerPoint PPT Presentation

1 / 70
About This Presentation
Title:

Microsoft VB 2005: Reloaded, Advanced

Description:

Allows you to limit the user to only one choice in a group of two or more choices ... Custom data types give the programmer more control and flexibility in defining ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 71
Provided by: cecsC
Category:

less

Transcript and Presenter's Notes

Title: Microsoft VB 2005: Reloaded, Advanced


1
Microsoft VB 2005 Reloaded, Advanced
  • Chapter 2
  • Reviewing Microsoft Visual Basic 2005 Reloaded
    Part II

2
Objectives
  • Manipulate characters within strings
  • Add radio buttons and check boxes to user
    interfaces
  • Create Sub procedures and Function procedures
    within applications

3
Objectives (continued)
  • Create and manipulate one-dimensional and
    two-dimensional arrays
  • Create a structure and manipulate structure
    variables
  • Read information from and write information to
    sequential access files

4
String Manipulation and More Controls
  • Objective
  • Explore ways of examining and manipulating
    strings

5
Determining the Number of Characters Contained in
a String
6
Removing Characters from a String
7
The Remove Method
  • Removes one or more characters located anywhere
    in a string

8
The Remove Method (continued)
9
Replacing Characters in a String
  • The Replace method
  • Replaces a sequence of characters in a string
    with another sequence of characters
  • The Mid statement
  • Replaces a specified number of characters in an
    existing string with characters from another
    string

10
Replacing Characters in a String (continued)
11
Replacing Characters in a String (continued)
12
Inserting Characters in a String
  • The PadLeft and PadRight methods
  • Both methods pad the string with a character
    until the entire string is a specified length
  • The Insert method
  • Inserts characters anywhere within a string

13
Inserting Characters in a String (continued)
14
Inserting Characters in a String (continued)
15
Searching a String for Characters
  • The StartsWith and EndsWith methods
  • Substring
  • Contiguous sequence of characters taken from a
    string
  • Determines whether a specific substring occurs at
    the beginning or end of a string
  • The Contains method
  • Determines whether a string contains a specific
    sequence of characters
  • The IndexOf method
  • Determines where substring might be in a string

16
Searching a String for Characters (continued)
17
Searching a String for Characters (continued)
18
Searching a String for Characters (continued)
19
Accessing Characters Contained in a String
20
Accessing Characters Contained in a String
(continued)
21
Comparing Strings
  • The String.Compare method
  • Compares strings
  • Uses word sort rules when comparing the strings
  • Numbers are considered less than lowercase
    letters, which are considered less than uppercase
    letters
  • The Like operator
  • Allows you to use pattern-matching characters to
    determine whether a string matches a specified
    pattern

22
Comparing Strings (continued)
23
Comparing Strings (continued)
24
Comparing Strings (continued)
25
Comparing Strings (continued)
26
More Controls
  • Using radio buttons
  • RadioButton control
  • Allows you to limit the user to only one choice
    in a group of two or more choices
  • GroupBox controls
  • Used so that only one radio button can be
    selected in each group
  • Important properties
  • Checked
  • Name
  • Text

27
More Controls (continued)
28
More Controls (continued)
  • Using check boxes
  • CheckBox tool
  • Allows the user to select any number of choices
    from a group of one or more choices

29
More Controls (continued)
30
Sub and Function Procedures
  • Procedure
  • Block of program code that performs a specific
    task
  • Function procedure
  • Returns a value after performing its assigned
    task
  • Sub procedure
  • Does not return a value

31
Sub Procedures
  • Types
  • Event procedure
  • Procedure associated with a specific object and
    event
  • Independent Sub procedure
  • Section of code that can be invoked from one or
    more places in an application
  • And is independent of any object and event
  • Parameter
  • Combination of data type and variable name in a
    parameter list
  • Stores the information passed to the procedure
    when it is called

32
Sub Procedures (continued)
33
Sub Procedures (continued)
34
Including Parameters in an Independent Sub
Procedure
  • Call statement
  • Uses the argument list to pass information to a
    procedure when necessary
  • The number of arguments listed in the argument
    list must agree with the number of parameters
    listed in the parameter list
  • As well as the data type and position of each
    parameter

35
Passing Variables to a Sub Procedure
  • Passing by value
  • Passes only a variables value
  • Include the keyword ByVal before the variables
    corresponding parameter
  • Passing by reference
  • Passes a variables memory address
  • Gives the receiving procedure total access to the
    variable
  • Include the keyword ByRef before the variables
    corresponding parameter

36
Associating a Procedure with Multiple Events
  • Event procedure header contains
  • Variable sender
  • Receives the memory address of the object that
    raised the event
  • Variable e
  • Contains additional event data provided by the
    object that raised the event
  • Handles clause
  • Indicates the objects that are associated with
    this event
  • Can include several objects and events

37
Function Procedures
  • Function
  • A block of code that performs a specific task and
    returns a value after completing the task
  • Visual Basic has many built-in functions
  • The Pine Lodge application
  • Demonstrates two techniques
  • Associating a procedure with multiple events
  • Using a Function procedure

38
Function Procedures (continued)
39
Function Procedures (continued)
40
Arrays
  • Simple variable (also called scalar variable)
  • A variable that is unrelated to any other
    variable
  • Array
  • Group of related variables

41
Using Arrays
  • Array
  • Group of variables that is given a name
  • All variables in the group have the same data
    type
  • Most common types of arrays
  • One-dimensional arrays
  • Two-dimensional arrays

42
One-Dimensional Arrays
  • One-dimensional array
  • A column (or row) of variables
  • Subscript
  • Identifies each variable (referred to as an
    element) in a one-dimensional array
  • The first element in a one-dimensional array has
    a subscript of 0 (zero)
  • Before you can use an array, you first must
    declare (create) it

43
One-Dimensional Arrays (continued)
44
One-Dimensional Arrays (continued)
45
Storing Data in a One-Dimensional Array
46
Manipulating One-Dimensional Arrays
  • Displaying the contents of a one-dimensional
    array
  • You can use a ForNext loop
  • The For EachNext statement
  • Accesses each element in a group without
    explicitly using the subscripts of the group
    elements
  • Calculating the average of one-dimensional
    numeric array elements
  • arrayName.Length returns the number of elements
    in arrayName

47
Manipulating One-Dimensional Arrays (continued)
48
Manipulating One-Dimensional Arrays (continued)
  • Determining the highest value stored in a
    one-dimensional array
  • Assign the first element of the array as the
    highest
  • Compare the highest with the remaining elements
  • Updating the highest every time a higher value is
    found
  • Sorting the data stored in a one-dimensional
    array
  • Array.Sort method sorts the elements in a
    one-dimensional array in ascending order
  • Array.Reverse method reverses the array elements

49
Parallel One-Dimensional Arrays
  • Parallel arrays
  • Two or more arrays whose elements are related by
    their positions in the arrays
  • Each corresponding element of the separate arrays
    belongs to a single entity

50
Two-Dimensional Arrays
  • Two-dimensional array
  • Can be viewed as a table of values with a fixed
    number of rows and columns
  • Storing data in a two-dimensional array
  • arrayName(rowSubscript, columnSubscript) value
  • Searching a two-dimensional array
  • You must iterate through the array using row and
    column subscripts

51
Two-Dimensional Arrays (continued)
52
Two-Dimensional Arrays (continued)
53
Structures and Sequential Access Files
  • Structure statement
  • Allows you to create your own custom data types
    in Visual Basic
  • Custom data types give the programmer more
    control and flexibility in defining and
    describing entities used in a program

54
Structures
  • Structure (also called a user-defined data type)
  • Data type consisting of a group of different
    members
  • Structure members
  • Can be variables, constants, or procedures
  • Variables are referred to as member variables
  • Structure statement
  • Allows you to create a structure in Visual Basic

55
Structures (continued)
56
Structures (continued)
  • Using a structure to declare a variable
  • Structure variables
  • Variables declared using a structure
  • Syntax
  • Dim Private structureVariableName As
    structureName
  • To refer to an individual member variable
  • structureName.memberVariableName
  • Passing a structure variable to a procedure
  • Structure variables are passed by value

57
Structures (continued)
  • Creating an array of structure variables
  • Create a new structure
  • Declare a one-dimensional array of the type of
    the structure

58
File Types
  • Input file
  • Get information by reading it (reading the
    file)
  • Output file
  • Send information to it (write to the file)
  • Sequential access file
  • Accessed in sequence from beginning to end
  • Random access file
  • Accessed directly without going from beginning to
    end
  • Binary access file
  • Can be accessed by byte location in the file

59
Sequential Access File
  • Also referred to as a text file
  • It is composed of lines of human-readable text
  • Data can be accessed only sequentially

60
Writing Information to a Sequential Access File
61
Aligning Columns of Information in a Sequential
Access File
  • PadLeft and PadRight methods also apply to
    strings written to sequential access files

62
Reading Information from a Sequential File
63
Determining Whether a File Exists
  • An error occurs when the computer attempts to
    read a sequential access file that does not exist
  • The FileExists method
  • Determines whether the file exists

64
The FormClosing Event
  • Occurs just before a form is about to be closed
  • This happens
  • When the computer processes the Me.Close()
    statement in the forms code
  • When the user clicks the Close button on the
    forms title bar

65
The FormClosing Event (continued)
66
The FormClosing Event (continued)
67
The Friends Application
68
Summary
  • Strings Length property
  • Determines the number of characters in the string
  • Some string methods
  • TrimStart, TrimEnd, Trim
  • Remove
  • Replace
  • Mid
  • PadLeft, PadRight
  • Insert
  • StartsWith, EndsWith

69
Summary (continued)
  • Some string methods
  • Contains
  • IndexOf
  • SubString
  • String.Compare
  • Like
  • Other controls
  • Radio buttons
  • Check boxes

70
Summary (continued)
  • Types of procedures
  • Sub
  • Function
  • Call statement invokes an independent Sub
    procedure
  • Passing parameters
  • By value
  • By reference
  • Array types
  • One-dimensional and two-dimensional
Write a Comment
User Comments (0)
About PowerShow.com