Title: Processing Text Files with Arrays and Lists
1Chapter 7
- Processing Text Files with Arrays and Lists
2Objectives
- Learn about processing textual data
- Open, read, and close a sequential file
- Use arrays to manage lists of data
- Parse input lines
- Sort arrays
- Write to a sequential file
- Group items using structures
- Create instances of the ComboBox and ListBox
controls
3Processing Textual Data
- Many applications work with text files
- Word, Excel, and others
- Text files are read and written from beginning to
end - This strategy is called sequential access
- Sequential files are also called ASCII-delimited
files
4Characteristics of Sequential Files
- Contents are separated into lines
- Each line ends with a carriage return
- Also known as a hard carriage return
- Pressing the Enter key generates a carriage
return in most applications - A line is called a record
- A record contains a logical group of items
- A record contains one or more fields
- Each field contains a unique piece of information
from a record - Each field is separated by a delimiter
- A comma is the most common delimiter character
5Caveats of Sequential Files
- Make sure that each record contains the correct
number of fields - If a field contains no information, place two
delimiters next to one another - Make sure that each line ends with a carriage
return - Last line must end with a carriage return
- Make sure that no extra carriage returns exist at
the end of the file - Formatting errors will likely cause VB .NET to
raise an exception - Note that sequential file processing has changed
considerably in VB .NET
6Sequential File (Example)
Beets Joe,131.216.39.3,Truman Bill,182.219.42.4,44
8
Deems Mary,131.216.39.3,Truman Bill,181.219.42.4,2
88
Five fields separated by commas (delimiter)
7Working with Sequential Files
- The System.IO namespace supplies two classes to
read and write sequential files - The StreamReader class reads sequential files
- The StreamWriter class writes sequential files
8Opening a Sequential File
- To read a sequential file you must first open it
- Open a file by creating an instance of the
StreamReader class - Standard prefix for the StreamReader class is
"srd" - Syntax
- Overloads Public Sub New (path As String )
- path argument contains the path and file name of
the file to open
9Opening a Sequential File (Example)
- Open the file name stored in the instance of the
OpenFileDialog control named ofdCommon - Dim psrdCurrent As System.IO.StreamReader
- psrdCurrent New _ System.IO.StreamReader(ofdComm
on.FileName)
10Reading a Sequential File
- After opening a sequential file, you read it
- You typically use a loop to read a sequential
file - Use the methods of the StreamReader class to read
the file - Read method reads a single character or multiple
characters - ReadLine method reads a line into a string
- Line is terminated by a carriage return
- ReadToEnd method reads from its current position
to the end of the file - The Read method returns Nothing when the file has
been read
11Reading a Sequential File (Example)
- Assuming that psrdCurrent is an instance of the
StreamReader class, the following loop reads the
sequential file. The contents are stored in the
string pstrFile - Dim pstrLine, pstrFile As String
- pstrLine psrdCurrent.ReadLine()
- Do Until pstrLine Nothing
- pstrFile pstrLine CrLf
- pstrLine psrdCurrent.ReadLine()
- Loop
12Logic to Read a Sequential File
Read first line
pstrLine psrdCurrent.Readline()
Does input line contain data?
Do Until pstrLine Nothing
no
yes
pstrFile pstrLine CrLf
Process current line
pstrLine psrdCurrent.Readline()
Read next input line
Loop
Exit loop
13Closing a File
- After reading a file, you must explicitly close
it - Failure to explicitly close a file may cause file
corruption or loss of data (especially when
writing a file - Call the Close method of the StreamReader class
- Example (Assume psrdCurrent is an instance of the
StreamReader class) - psrdCurrent.Close()
14Passing Arguments to a Constructor
- Remember that a constructor is a procedure named
New that executes when VB .NET creates a form or
other class instance - Any constructor can accept arguments
- Argument list works the same as the argument list
for any function or sub procedure - Public Sub New(ByVal pstrInput As String)
- MyBase.New()
- InitializeComponent()
- txtInputFile.Text pstrInput
- End Sub
15Introduction to Arrays
- Variables storing a single item of information
are called scalar variables - Integer, Double, and so on
- Arrays on the other hand store lists of data
- Lists of Integers, Doubles, and so on
16General Information about Arrays
- An array stores a list of several items each
having the same data type - An array has one or more dimensions
- The number of dimensions in an array is called
the rank - An array with one dimension has a rank of 1
- Each data item in an array is called an element
- Each individual element is referenced by a unique
index number known as a subscript - In VB .NET, arrays are classes (reference types)
supporting properties and methods
17Comparing Scalar Variables with an Array
twelve variables for months of the year
mintJanuary
mintFebruary
mintMarch
mintDecember
18A Note About VB 6 Arrays
- In VB 6, arrays were not classes. Thus they did
not support properties or methods - VB 6 arrays could have a lower bound other than 0
- All VB .NET arrays have a lower bound of 0
19The Array Class
- Properties
- Length Gets the total number of elements in the
array - Rank Gets the number of dimensions in the array
- Methods
- GetLowerBound Gets the smallest subscript for
an array dimension - Value is always 0
- GetUpperBound Gets the largest subscript for an
array dimension - Reverse Reverses the elements in a
one-dimensional array - Sort Sorts an array in ascending order
20Common Array Operations
- You declare an array the same way as you declare
a scalar variable - You store and retrieve individual elements from
an array - Arrays can grow and shrink at run time
- You can change the number of array dimensions or
the number of elements in an array dimension - You must often search for a particular element in
an array - The elements of an array often need to be sorted
21Declaring Arrays
- When you declare an array, you are allocating
memory (RAM) to store the array - Remember that arrays are reference types so an
array variable stores a memory address - To declare an array, you use the Dim, Private or,
Public statements just as you would use to
declare any variable
22Declaring Arrays (Syntax)
- Dim Private Public array-name
- ( size ) As datatype initexpr
- The Dim, Private, and Public keywords define the
scope of the array - Same meaning as a scalar variable
- Array-name can be any valid variable name
- Optional size argument defines the initial number
of array elements - If omitted, the array has no initial size
- As datatype clause defines the data type of each
element - Required when strict type checking is enabled
- Initexpr allows you to assign values to each
element when the array is declared
23Declaring Arrays (Example)
- Declare an empty array having a data type of
Integer - Private mintEmpty() As Integer
- Declare an array with 4 elements having a data
type of Integer, subscripted from 0 to 3 - Private mintValues(3) As Integer
- Declare an array with 5 elements having a data
type of Single, subscripted from 0 to 4 - Private msngValues(4) As Single
- (see testArray.sln)
24Declaring and Initializing Arrays (1)
- Syntax differs because you are not initializing a
scalar variable but rather elements in a list - You initialize an array by entering values in a
comma separated list. The list is enclosed in
braces - Declare a dynamic Integer array named pintList
and initialize the array elements - Dim pintList() As Integer 24, 12, 34, 42
25Declaring and Initializing Arrays (2)
- String arrays can also be declared and
initialized - As with all strings, literal values are enclosed
in double quotation marks
Private mstrMonthNames() As String _
"January", "February", "March", "April", _
"May", "June", "July", "August", _
"September", "October", "November", _
"December"
26Redimensioning Arrays
- The process of changing an array's size is known
as redimensioning an array - All VB .NET arrays are dynamic so any array can
be redimensioned - Arrays are redimensioned with the ReDim statement
- As an executable statement, ReDim must appear
inside of a procedure - You can change the size of an array but you
cannot change an array's data type
27The ReDim Statement (Syntax)
- ReDim Preserve varname( size )
- The ReDim statement resizes an array
- The optional Preserve keyword preserves the
existing contents - When using the Preserve keyword, you can only
change the size of the last dimension - When reducing the size, elements will be deleted
- varname contains the array to resize
- size argument contains the new size (dimension)
28The ReDim Statement (Example)
- Resize an array and destroy its contents
- ReDim mintMonths(11)
- Resize an array and preserve its contents
- ReDim Preserve mintMonths(11)
29Performing Assignment Statements with Arrays (1)
- Assignment process is the same as with ordinary
variables - However you assign individual elements rather
than the entire array - Store the value 84616 in the first array element
- Remember that all arrays in VB .NET are 0-based
- mintMailMessages(0) 84616
30Performing Assignment Statements with Arrays (2)
- Arrays and scalar variables can be declared as
the following statements illustrate - Private mintMailMessages(11) As Integer
- Private mintMailMessageItem As Integer
- Store the first array element in the scalar
variable named mintMailMessageItem - mintMailMessageItem mintMailMessage(0)
31Type Conversion with Arrays
- You can use type conversion methods with arrays
just as you use them with scalar variables - Convert an Integer array element to a string
- Private mintMailMessages(11) As Integer
- txtMailMessageMonth.Text _
mintMailMessage(0).ToString
32Common Array Errors (1)
- The size and shape of an array defines the array
bounds - Subscript values cannot exceed the array bounds
- The following statement will cause an error
because the array has 12 elements but the second
statement tries to reference element number 500 - Private mintMailMessage(11) As Integer
- mintMailMessage(500) 100
33Common Array Errors (2)
- To store data in an array element, you must use a
subscript - The following assignment statement will cause an
error because it does not specify an array
element - Private mintMailMessages(11) As Integer
- mintMailMessages 100
34Working with Subscripts
- Both variables and literal values can be used as
subscripts - When using variables, the variable should be
declared as Short, Integer, or Long - Example
- Dim pintValue As Integer 3
- txtCount.Text mintMailMessages(pintValue).ToStr
ing
35Arrays and Loops
- Loops are commonly used to examine all of the
elements in an array or to store a value in all
of the array elements - Example to calculate a total
- Note that pintTotal is an accumulator
- Dim pintCount, pintTotal As Integer
- For pintCount 0 to 11
- pintTotal mintMailMessages(pintCount)
- Next
36The Split Method (1)
- When you read a record from a sequential file you
must often split the line into its component
fields - This process is called parsing
- The Split method divides a string into an array
of strings based on a delimiter character or
characters - Most files use the comma (,) as the delimiter
character but other characters can be used
37The Split Method (2)
- The Split method requires that you declare an
array having a data type of Char having one
element - The following statement declares and initializes
an array of type Char -
- Dim pchrDelimiter() As Char ToChar.(",")
38The Split Method (Syntax)
- Overloads Public Function Split( ByVal separator(
) As Char ) As String( ) - The Split method parses a string into fields
- The separator argument contains an array of
characters - Usually just a single character though
- The Split method returns an array of strings
39The Split Method (Example)
- Assume that pstrLine contains the string to be
parsed - Dim pstrLine As String
- Dim pstrFields() As String
- Dim pchrDelimiter As Char() ToChar(",")
- pstrFields pstrLine.Split(pchrDelimiter)
40Splitting a Text Line
pstrFields pstrLine.Split(pchrDelimiter)
Comma (,)
Deems Mary,131.216.39.3,Truman Bill,181.219.42.4,2
88
41Sorting Arrays (1)
- You sort an array by calling the Sort method of
the Array class - The overloaded Sort method has multiple syntax
variations - Overloads Public Shared Sub Sort( array As Array
) - Overloads Public Shared Sub Sort( keys As Array,
items As Array )
42Sorting Arrays (2)
- The first overloaded method sorts a
one-dimensional array in ascending order - The second overloaded method sorts two arrays
- First array contains the keys
- Second array contains the values
- Exchanges to the keys array are made to the
values array
43Sorting Arrays (Example)
- Sort the array named mintMailMessages
- System.Array.Sort(mintMailMessages)
- Sort the arrays named mintMailMessages and
mstrMonthNames - mintMailMessages contains the keys and
mstrMonthNames contains the values - System.Array.Sort(mintMailMessages,
mstrMonthNames)
44Writing to a File
- Just as the StreamReader class reads a file, the
StreamWriter class writes a file - Properties
- The NewLine property contains the character(s)
that mark the end of a line. - When calling WriteLine, this character is
appended to the line - Methods
- Close method closes a file
- Write method write a character, character array,
or string to a file - WriteLine method writes a line terminated with a
NewLine character
45Writing to a File (Example)
- Write two arrays to a file
- Assume pswriCurrent is an instance of the
StreamWriter class - For pintCount 0 to 11
- pswriCurrent.Write(mstrMonthNames(pintCount))
- pswriCurrent.Write(",")
- pswriCurrent.Write(mintMailMessages(pintCount))
- pswriCurrent.WriteLine()
- Next
- pswriCurrent.Close()
46Structures
- Structures group related data times together
logically - For example, a customer has a name, address,
city, state, and zip code - A structure allows you to create your own data
type containing elements made up of other data
items
47Structures (Syntax)
- Public Private Structure varname
- NonMethod Declarations
- Method Declarations
- End Structure
- Public and Private keywords define the scope of
the structure - varname contains the name of the structure
- Standard variable naming rules apply
- NonMethod declarations contain the structure
members
48Structures (Example)
- Declare a structure named sLogRecord
- Public Structure sLogRecord
- Public SourceName As String
- Public SourceIP As String
- Public DestName As String
- Public DestIP As String
- Public Bytes As Integer
- End Structure
49Declaring a Structure Variable
- Once you have declared a type with the Structure
statement, you can declare variables of that type - Use the same syntax to declare a Structure
variable as an ordinary variable - Declare a structure named LogRecord
- Private LogRecord As sLogRecord
- Declare an array of structures named LogRecords
- Private LogRecords() As sLogRecord
50Referencing Structures (1)
- To reference an individual structure member use
the same dot (.) notation that you use to
reference the properties of an object - Note that Intellisense technology works on
structures too - Dim LogRecord As sLogRecord
- LogRecord.SourceName "Mary"
51Referencing Structures (2)
- You can work with arrays of structures too
- The key is that both sides of an assignment
statement must have the same data type - Private LogRecords(99) As sLogRecord
- Private LogRecord As sLogRecord
- LogRecord LogRecords(0)
- LogRecord.SourceName LogRecords(0).SourceName
52The ComboBox and ListBox Controls
- Both controls display lists of data, such as a
list of names - Both controls allow you to sort data and
determine which item the user has selected from a
list of items - The primary difference is that a ComboBox control
will drop-down but a list box will not - Both controls share almost the same set of
properties
53ComboBox Styles
- DropDownStyle property controls how the ComboBox
appears to the user - DropDown Allows user to select an item from a
drop-down list of items - The user can also add a new item
- Simple Control instance appears like a list box
- List does not drop-down
- DropDownList Same as DropDown but user cannot
add new items
54ComboBox and ListBox Properties
- Items property defines the items stored in the
ComboBox or ListBox - MaxDropDownItems controls maximum number of items
that will appear in the drop-down list - The 0-based SelectedIndex property defines which
item the user has selected - The value is -1 if no item is selected
- Sorted property if True causes the items to be
sorted alphabetically - Text property contains the text of the currently
selected item
55ComboBox and ListBox Events
- SelectedIndexChanged event fires when the user
selects a different item in the ComboBox or
ListBox - TextChanged event fires when the user changes the
text of the current item
56The Items Collection (General)
- Applies to both the ComboBox and ListBox controls
- Contains the items that appear in the control
instance - Similar to an array but contains additional
methods to locate an item, add new items, and
remove existing ones - Items collection is but one collection among many
supported by VB .NET
57The Items Collection
- Properties
- Count property returns the number of items in a
collection - Item property retrieves a specific element from
the collection - Collection is 0-based
- Methods
- Add method adds a new item to the collection
- Clear method removes all items from the
collection - RemoveAt method removes a single item
58Adding Items to a Collection
- The following For loops add items to the Items
collection using the array of LogRecord
structures named LogRecords - For pintCount 0 To LogRecords.GetUpperBound(0)
- cboSourceIP.Items.Add(LogRecords(pintCount).So
urceName - Nexts
59The End!