Title: Microsoft VB 2005: Reloaded, Advanced
1Microsoft VB 2005 Reloaded, Advanced
- Chapter 2
- Reviewing Microsoft Visual Basic 2005 Reloaded
Part II
2Objectives
- Manipulate characters within strings
- Add radio buttons and check boxes to user
interfaces - Create Sub procedures and Function procedures
within applications
3Objectives (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
4String Manipulation and More Controls
- Objective
- Explore ways of examining and manipulating
strings
5Determining the Number of Characters Contained in
a String
6Removing Characters from a String
7The Remove Method
- Removes one or more characters located anywhere
in a string
8The Remove Method (continued)
9Replacing 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
10Replacing Characters in a String (continued)
11Replacing Characters in a String (continued)
12Inserting 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
13Inserting Characters in a String (continued)
14Inserting Characters in a String (continued)
15Searching 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
16Searching a String for Characters (continued)
17Searching a String for Characters (continued)
18Searching a String for Characters (continued)
19Accessing Characters Contained in a String
20Accessing Characters Contained in a String
(continued)
21Comparing 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
22Comparing Strings (continued)
23Comparing Strings (continued)
24Comparing Strings (continued)
25Comparing Strings (continued)
26More 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
27More Controls (continued)
28More Controls (continued)
- Using check boxes
- CheckBox tool
- Allows the user to select any number of choices
from a group of one or more choices
29More Controls (continued)
30Sub 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
31Sub 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
32Sub Procedures (continued)
33Sub Procedures (continued)
34Including 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
35Passing 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
36Associating 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
37Function 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
38Function Procedures (continued)
39Function Procedures (continued)
40Arrays
- Simple variable (also called scalar variable)
- A variable that is unrelated to any other
variable - Array
- Group of related variables
41Using 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
42One-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
43One-Dimensional Arrays (continued)
44One-Dimensional Arrays (continued)
45Storing Data in a One-Dimensional Array
46Manipulating 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
47Manipulating One-Dimensional Arrays (continued)
48Manipulating 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
49Parallel 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
50Two-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
51Two-Dimensional Arrays (continued)
52Two-Dimensional Arrays (continued)
53Structures 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
54Structures
- 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
55Structures (continued)
56Structures (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
57Structures (continued)
- Creating an array of structure variables
- Create a new structure
- Declare a one-dimensional array of the type of
the structure
58File 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
59Sequential Access File
- Also referred to as a text file
- It is composed of lines of human-readable text
- Data can be accessed only sequentially
60Writing Information to a Sequential Access File
61Aligning Columns of Information in a Sequential
Access File
- PadLeft and PadRight methods also apply to
strings written to sequential access files
62Reading Information from a Sequential File
63Determining 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
64The 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
65The FormClosing Event (continued)
66The FormClosing Event (continued)
67The Friends Application
68Summary
- 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
69Summary (continued)
- Some string methods
- Contains
- IndexOf
- SubString
- String.Compare
- Like
- Other controls
- Radio buttons
- Check boxes
70Summary (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