Skip to content

4. Commonly Used .NET Classes

Here we present a few classes from the .NET platform that are of interest, even for a beginner. First, we show how to obtain information about the hundreds of available classes.

4.1. Seeking help with SDK.NET

4.1.1. wincv

If you have installed only the SDK and not Visual Studio.NET, you can use the wincv.exe program, which is typically located in the SDK directory, for example C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1. When you launch this utility, you see the following interface:

Type the name of the desired class in (1). This returns various possible options in (2). Select the appropriate one, and the result appears in (3)—in this case, the HashTable class. This method is suitable if you know the name of the class you are looking for. If you want to explore the list of options offered by the .NET platform, you can use the HTML file StartHere.htm, which is also located directly in the SSK.Net installation folder, for example C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.

Image

The .NET Framework SDK Documentation link is the one to follow to get an overview of .NET classes:

Image

From there, click the [Class Library] link. It contains a list of all .NET classes:

Image

For example, let’s follow the System.Collections link. This namespace contains various classes that implement collections, including the HashTable class:

Image

Let’s follow the HashTable link below:

Image

We arrive at the following page:

Image

Note the position of the hand below. It is pointing to a link that allows you to specify the desired language, in this case Visual Basic.

Here you’ll find the class prototype as well as usage examples. Click the [Hashtable, Members] link below:

Image

We get the complete description of the class:

Image

This method is the best way to explore the SDK and its classes. The WinCV tool is useful when you already know a little about the class but have forgotten some of its members. WinCV then allows you to quickly find the class and its members.

4.2. Searching for help on classes with VS.NET

Here are some tips for finding help with Visual Studio.NET

4.2.1. Help Option

Select the [?] option from the menu.

Image

The following window appears:

Image

In the drop-down list, you can select a help filter. Here, we’ll choose the [Visual Basic] filter.

Image

Two types of help are useful:

  • help on the VB.NET language itself (syntax)
  • help on the .NET classes that can be used by the VB.NET language

The VB.NET language help is accessible via [Visual Studio.NET/Visual Basic and Visual C#/Reference/Visual Basic]:

Image

This brings up the following help page:

Image

From there, the various subheadings provide help on different VB.NET topics. Pay special attention to the VB.NET tutorials:

Image

To access the various classes of the .NET platform, select the [Visual Studio.NET/.NET Framework] help.

Image

We’ll focus in particular on the [Reference/Class Library] section:

Image

Suppose we are interested in the [ArrayList] class. It is located in the [System.Collections] namespace. It is important to know this; otherwise, we would prefer the search method described below. We get the following help:

Image

The [ArrayList, Class] link provides an overview of the class:

Image

This type of page exists for all classes. It provides a summary of the class with examples. For a description of the class members, follow the [ArrayList, members] link:

Image

4.2.2. Help/Index

Image

The [Help/Index] option allows you to search for more targeted help than the previous method. Simply type in the keyword you are looking for:

Image

The advantage of this method over the previous one is that you don’t need to know where to find what you’re looking for in the help system. This is likely the preferred method for targeted searches, while the other method is better suited for exploring everything the help system has to offer.

4.3. The String Class

The String class has many properties and methods. Here are a few of them:

Public ReadOnly Property Length As Integer
number of characters in the string
Public Default ReadOnly Property
Chars(ByVal index As Integer) As Char
default indexed property. [String].Chars(i) is the i-th character of the string
Public Function EndsWith(ByVal value As String)
As Boolean
Returns true if the string ends with value
Public Function StartsWith(ByVal value As String)
As Boolean
Returns true if the string starts with value
Overloads Public Function Equals(ByVal value
As String) As Boolean
returns true if the string is equal to value
Overloads Public Function IndexOf(ByVal value
As String) As Integer
returns the first position in the string where the string value is found—the search starts from character 0
Overloads Public Function IndexOf(ByVal value
As String, ByVal startIndex As Integer) As Integer
Returns the position of the first occurrence of the string value in the string—the search starts at character index startIndex
Overloads Public Shared Function
Join(ByVal separator As String, ByVal value()
As String) As String
class method - returns a string resulting from concatenating the values in the value array with the separator separator
Overloads Public Function Replace(ByVal oldChar
As Char, ByVal newChar As Char) As String
returns a copy of the current string where the character oldChar has been replaced by the character newChar
Overloads Public Function Split(ByVal ParamArray
separator() As Char) As String()
The string is treated as a sequence of fields separated by the characters in the separator array. The result is an array of these fields
Overloads Public Function Substring(
ByVal startIndex As Integer, ByVal length As Integer)
As String
A substring of the current string starting at position startIndex and containing length characters
Overloads Public Function ToLower()
As String
converts the current string to lowercase
Overloads Public Function ToUpper()
As String
converts the current string to uppercase
Overloads Public Function Trim()
As String
removes leading and trailing spaces from the current string

A C string can be considered an array of characters. Thus

  • C.Chars(i) is the i-th character of C
  • C.Length is the number of characters in C

Consider the following example:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System

Module test
    Sub Main()
        Dim aString As String = "the bird flies above the clouds"
        display("aString=" + aString)
        display("aString.Length=" & aString.Length)
        display("string[10]=" + aString.Chars(10))
        display("aString.IndexOf(""flies"")=" & aString.IndexOf("flies"))
        display("aString.IndexOf(""x"")=" & aString.IndexOf("x"))
        display("aString.LastIndexOf('a')=" & aString.LastIndexOf("a"c))
        display("aString.LastIndexOf('x')=" & aString.LastIndexOf("x"c))
        display("aString.Substring(4,7)=" + aString.Substring(4, 7))
        display("aString.ToUpper()=" + aString.ToUpper())
        display("aString.ToLower()=" + aString.ToLower())
        display("aString.Replace('a','A')=" + aString.Replace("a"c, "A"c))
        Dim fields As String() = aString.Split(Nothing)
        Dim i As Integer
        For i = 0 To fields.Length - 1
            display("fields[" & i & "]=[" & fields(i) & "]")
        Next i
        display("Join("":"",fields)=" + System.String.Join(":", fields))
        display("(""  abc  "").Trim()=[" + "  abc  ".Trim() + "]")
    End Sub

    ' display
    Sub display(ByVal msg As [String])
        ' display msg
        Console.Out.WriteLine(msg)
    End Sub
End Module

Execution yields the following results:

dos>vbc string1.vb

dos>string1
aString = "the bird flies above the clouds"
aString.Length = 34
string[10] = o
aString.IndexOf("flies")=9
aString.IndexOf("x")=-1
aString.LastIndexOf('a')=30
aString.LastIndexOf('x')=-1
aString.Substring(4,7) = "seau vo"
aString.ToUpper() = THE BIRD FLIES ABOVE THE CLOUDS
aString.ToLower() = the bird flies above the clouds
aString.Replace('a','A') = theBird flies Above the clouds
fields[0] = [the bird]
fields[1] = [flies]
fields[2] = [above]
fields[3] = [of]
fields[4] = [clouds]
Join(":",fields) = the bird:flies:above:the:clouds
("abc").Trim() = [abc]

Let's consider a new example:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System

Module string2
    Sub Main()
        ' the line to be analyzed
        Dim line As String = "one:two::three:"
        ' field separators
        Dim separators() As Char = {":"c}
        ' split
        Dim fields As String() = line.Split(separators)
        Dim i As Integer
        For i = 0 To champs.Length - 1
            Console.Out.WriteLine(("Fields[" & i & "]=" & fields(i)))
        Next i
        ' join
        Console.Out.WriteLine(("join=[" + System.String.Join(":", champs) + "]"))
    End Sub
End Module

and the execution results:

Fields[0]=one
Fields[1]=two
Fields[2]=
Fields[3]=three
Fields[4]=
join=[one:two::three:]

The Split method of the String class allows you to place the fields of a string into an array. The definition of the method used here is as follows:

Overloads Public Function Split(ByVal ParamArray separator() As Char) As String()
separator
array of characters. These characters represent the characters used to separate the fields in the string. Thus, if the string is [field1, field2, field3], we can use separator=new char() {","c}. If the separator is a sequence of spaces, we use separator=nothing.
result
array of strings where each element is a field of the string.

The Join method is a static method of the String class:

Overloads Public Shared Function Join(ByVal separator As String, ByVal value() As String) As String
value
array of strings
separator
a string that will serve as the field separator
result
a string formed by concatenating the elements of the value array, separated by the separator string.

4.4. The Array Class

The Array class implements an array. In our example, we will use the following properties and methods:

Public ReadOnly Property Length As Integer
property - number of elements in the array
Overloads Public Shared Function BinarySearch
(ByVal array As Array, ByVal index As Integer,
ByVal length As Integer, ByVal value As Object) As Integer
Class method - returns the position of value in the sorted array - searches starting from position index and with length elements
Overloads Public Shared Sub Copy(ByVal sourceArray
As Array, ByVal destinationArray As Array,
ByVal length As Integer)
Class method - copies the first length elements of sourceArray into destinationArray - destinationArray is created specifically for this purpose
Overloads Public Shared Sub Sort(ByVal array As Array)
Class method - sorts the array - the array must contain a data type with a default sorting function (strings, numbers).

The following program illustrates the use of the Array class:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System

Module test
    Sub Main()
        ' reading array elements entered from the keyboard
        Dim finished As [Boolean] = False
        Dim i As Integer = 0
        Dim elements1 As Double() = Nothing
        Dim elements2 As Double() = Nothing
        Dim element As Double = 0
        Dim response As String = Nothing
        Dim error As [Boolean] = False

        While Not finished
            ' question
            Console.Out.Write(("Element (real) " & i & " of the array (nothing to end): "))
            ' read the answer
            response = Console.ReadLine().Trim()
            ' end input if string is empty
            If response.Equals("") Then
                Exit While
            End If
            ' Check input
            Try
                element = [Double].Parse(response)
                error = False
            Catch
                Console.Error.WriteLine("Incorrect input, please try again")
                error = True
            End Try
            ' if no error
            If Not error Then
                ' new array to hold the new element
                elements2 = New Double(i) {}
                ' Copy the old array to the new array
                If i ≠ 0 Then
                    Array.Copy(elements1, elements2, i)
                End If
                ' The new array becomes the old array
                elements1 = elements2
                ' no longer need the new array
                elements2 = Nothing
                ' insert new element
                elements1(i) = element
                ' one more element in the array
                i += 1
            End If
        End While
        ' display unsorted array
        System.Console.Out.WriteLine("Unsorted array")
        For i = 0 To elements1.Length - 1
            Console.Out.WriteLine(("elements[" & i & "]=" & elements1(i)))
        Next i
        ' Sort the array
        System.Array.Sort(elements1)
        ' display sorted array
        System.Console.Out.WriteLine("Sorted array")
        For i = 0 To elements1.Length - 1
            Console.Out.WriteLine(("elements[" & i & "]=" & elements1(i)))
        Next i
        ' Search
        While Not finished
            ' Question
            Console.Out.Write("Element searched for (nothing to stop): ")
            ' Read and verify response
            response = Console.ReadLine().Trim()
            ' finished?
            If response.Equals("") Then
                Exit While
            End If
            ' verification
            Try
                element = [Double].Parse(response)
                error = False
            Catch
                Console.Error.WriteLine("Incorrect input, please try again")
                error = True
            End Try
            ' if no error
            If Not error Then
                ' search for the element in the sorted array
                i = System.Array.BinarySearch(elements1, 0, elements1.Length, element)
                ' Display the result
                If i >= 0 Then
                    Console.Out.WriteLine(("Found at position " & i))
                Else
                    Console.Out.WriteLine("Not in the array")
                End If
            End If
        End While
    End Sub
End Module

The screen output is as follows:

Element (real) 0 of the array (nothing to finish): 10.4
Array element (real) 1 (nothing to end with): 5.2
Array element (real) 2 (nothing to end with): 8.7
Array element (real) 3 (nothing to end with): 3.6
Element (real) 4 of the array (nothing to finish):
Unsorted array
elements[0]=10.4
elements[1]=5.2
elements[2]=8.7
elements[3]=3.6
Sorted array
elements[0]=3.6
elements[1]=5.2
elements[2]=8.7
elements[3]=10.4
Searched element (nothing to stop): 8.7
Found at position 2
Element searched for (nothing to stop): 11
Not in the array
Searched element (nothing to stop): a
Invalid input, please try again
Searched element (nothing to stop):

The first part of the program constructs an array from numerical data entered via the keyboard. The array cannot be predefined since we do not know how many elements the user will enter. We therefore work with two arrays, elements1 and elements2.


                ' new array to hold the new element
                elements2 = New Double(i) {}
                ' copy old array to new array
                If i <> 0 Then
                    Array.Copy(elements1, elements2, i)
                End If
                ' new array becomes old array
                elements1 = elements2
                ' no longer need the new array
                elements2 = Nothing
                ' insert new element
                elements1(i) = element
                ' one more element in the array
                i += 1

The elements1 array contains the currently entered values. When the user adds a new value, we create an elements2 array with one more element than elements1, copy the contents of elements1 into elements2 (Array.Copy), set elements1 to point to elements2, and finally add the new element to elements1. This is a complex process that can be simplified by using a variable-size array (ArrayList) instead of a fixed-size array (Array).

The array is sorted using the Array.Sort method. This method can be called with various parameters specifying the sorting rules. Without parameters, ascending sort is performed by default. Finally, the Array.BinarySearch method allows you to search for an element in a sorted array.

4.5. The ArrayList Class

The ArrayList class allows you to implement variable-size arrays during program execution, which the previous Array class does not allow. Here are some of the common properties and methods:

Public Sub New()
creates an empty list
Public Overridable ReadOnly Property Count
As Integer  Implements ICollection.Count
number of elements in the collection
Public Overridable Function Add(ByVal value
As Object) As Integer Implements IList.Add
Adds the value object to the end of the collection
Public Overridable Sub Clear()
Implements IList.Clear
clears the list
Overloads Public Overridable Function
IndexOf(ByVal value As Object) As Integer
Implements IList.IndexOf
index of the value object in the list or -1 if it does not exist
Overloads Public Overridable Function
IndexOf(ByVal value As Object, ByVal startIndex
As Integer) As Integer
Same as above, but starting from element startIndex
Overloads Public Overridable Function
LastIndexOf(ByVal value As Object) As Integer
Same as above, but returns the index of the last occurrence of value in the list
Overloads Public Overridable Function
LastIndexOf(ByVal value As Object, ByVal startIndex As Integer) As Integer
Same as above, but starting from the element at startIndex
Public Overridable Sub Remove(ByVal obj As Object)
Implements IList.Remove
Removes the object obj if it exists in the list
Public Overridable Sub RemoveAt(ByVal index As Integer)
Implements IList.RemoveAt
removes the element at index from the list
Overloads Public Overridable Function
BinarySearch(ByVal value As Object) As Integer
Returns the position of the value object in the list, or -1 if it is not found. The list must be sorted
Overloads Public Overridable Sub Sort()
Sorts the list. The list must contain objects with a predefined ordering relationship (strings, numbers)
Overloads Public Overridable Sub
Sort(ByVal comparer As IComparer)
sorts the list according to the ordering established by the comparer function

Let’s revisit the example using Array objects and apply it to ArrayList objects:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Collections

Test Module
    Sub Main()
        ' reading array elements entered from the keyboard
        Dim finished As [Boolean] = False
        Dim i As Integer = 0
        Dim elements As New ArrayList
        Dim element As Double = 0
        Dim response As String = Nothing
        Dim error As [Boolean] = False

        While Not finished
            ' question
            Console.Out.Write(("Element (real) " & i & " of the array (nothing to end): "))
            ' read the answer
            response = Console.ReadLine().Trim()
            ' end input if string is empty
            If response.Equals("") Then
                Exit While
            End If
            ' Check input
            Try
                element = Double.Parse(response)
                error = False
            Catch
                Console.Error.WriteLine("Incorrect input, please try again")
                error = True
            End Try
            ' if no error
            If Not error Then
                ' Add one more element to the array
                elements.Add(element)
            End If
        End While
        ' display unsorted array
        System.Console.Out.WriteLine("Unsorted array")
        For i = 0 To elements.Count - 1
            Console.Out.WriteLine(("elements[" & i & "]=" & elements(i).ToString))
        Next i        ' Sort the array
        elements.Sort()
        ' display sorted array
        System.Console.Out.WriteLine("Sorted array")
        For i = 0 To elements.Count - 1
            Console.Out.WriteLine(("elements[" & i & "]=" & elements(i).ToString))
        Next i
        ' Search
        While Not finished
            ' Query
            Console.Out.Write("Element searched for (nothing to stop): ")
            ' Read and verify response
            response = Console.ReadLine().Trim()
            ' Done?
            If response.Equals("") Then
                Exit While
            End If
            ' verification
            Try
                element = [Double].Parse(response)
                error = False
            Catch
                Console.Error.WriteLine("Incorrect input, please try again")
                error = True
            End Try
            ' if no error
            If Not error Then
                ' search for the element in the sorted array
                i = elements.BinarySearch(element)
                ' Display the result
                If i >= 0 Then
                    Console.Out.WriteLine(("Found at position " & i))
                Else
                    Console.Out.WriteLine("Not in the array")
                End If
            End If
        End While
    End Sub
End Module

The execution results are as follows:

Element (real) 0 of the array (nothing to finish): 10.4
Array element (real) 0 (nothing to finish): 5.2
Element (real) 0 of the array (nothing to finish): a
Invalid input, please try again
Element (real) 0 of the array (nothing to finish): 3.7
Element (real) 0 of the array (nothing to end with): 15
Element (real) 0 of the array (nothing to end with):
Unsorted array
elements[0]=10.4
elements[1]=5.2
elements[2]=3.7
elements[3]=15
Sorted array
elements[0]=3.7
elements[1]=5.2
elements[2]=10.4
elements[3]=15
Element searched for (nothing to stop on): a
Invalid input, please try again
Searched element (nothing to stop): 15
Found at position 3
Searched element (nothing to stop): 1
Not in the array
Searched element (nothing to stop):

4.6. The Hashtable class

The Hashtable class allows you to implement a dictionary. A dictionary can be viewed as a two-column array:

Keys are unique; that is, no two keys can be identical. The main methods and properties of the Hashtable class are as follows:

Public Sub New()
creates an empty dictionary
Public Overridable Sub Add(ByVal key As
Object, ByVal value As Object)
Implements IDictionary.Add
adds an entry (key, value) to the dictionary, where key and value are object references.
Public Overridable Sub Remove
(ByVal key As Object) Implements IDictionary.Remove
removes the entry with key=key from the dictionary
Public Overridable Sub Clear()
Implements IDictionary.Clear
clears the dictionary
Public Overridable Function ContainsKey
(ByVal key As Object) As Boolean
Returns true if the key key is in the dictionary.
Public Overridable Function
ContainsValue(ByVal value As Object) As Boolean
Returns true if the value value is in the dictionary.
Public Overridable ReadOnly Property
Count As Integer  Implements ICollection.Count
property: number of elements in the dictionary (key, value)
Public Overridable ReadOnly Property
Keys As ICollection Implements IDictionary.Keys
property: collection of dictionary keys
Public Overridable ReadOnly Property
Values As ICollection Implements IDictionary.Values
property: collection of dictionary values
Public Overridable Default Property
Item(ByVal key As Object) As Object  Implements IDictionary.Item
indexed property: allows you to retrieve or set the value associated with a key

Consider the following example program:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Collections

Test Module
    Sub Main()
        Dim list() As [String] = {"jean:20", "paul:18", "mélanie:10", "violette:15"}
        Dim i As Integer
        Dim fields As [String]() = Nothing
        Dim separators() As Char = {":"c}
        ' populating the dictionary
        Dim dictionary As New Hashtable
        For i = 0 To list.Length - 1
            fields = list(i).Split(separators)
            dico.Add(fields(0), fields(1))
        Next i
        ' number of elements in the dictionary
        Console.Out.WriteLine(("The dictionary has " & dic.Count & " elements"))
        ' list of keys
        Console.Out.WriteLine("[List of keys]")
        Dim keys As IEnumerator = dictionary.Keys.GetEnumerator()
        While keys.MoveNext()
            Console.Out.WriteLine(keys.Current)
        End While
        ' list of values
        Console.Out.WriteLine("[List of values]")
        Dim values As IEnumerator = dictionary.Values.GetEnumerator()
        While values.MoveNext()
            Console.Out.WriteLine(values.Current)
        End While
        ' List of keys & values
        Console.Out.WriteLine("[List of keys & values]")
        keys.Reset()
        While keys.MoveNext()
            Console.Out.WriteLine(("key=" & keys.Current.ToString & " value=" & dicto(keys.Current).ToString))
        End While
        ' We remove the key "paul"
        Console.Out.WriteLine("[Deleting a key]")
        dico.Remove("paul")
        ' list of keys & values
        Console.Out.WriteLine("[List of keys and values]")
        keys = dictionary.Keys.GetEnumerator()
        While keys.MoveNext()
            Console.Out.WriteLine(("key=" & keys.Current.ToString & " value=" & dictionary(keys.Current).ToString))
        End While

        ' Search in the dictionary
        Dim searchedName As [String] = Nothing
        Console.Out.Write("Search term (press Enter to continue): ")
        searchName = Console.ReadLine().Trim()
        Dim value As [Object] = Nothing
        While Not searchName.Equals("")
            If dictionary.ContainsKey(searchName) Then
                value = dictionary(searchName)
                Console.Out.WriteLine((searchName + "," + CType(value, [String])))
            Else
                Console.Out.WriteLine(("Name " + searchedName + " unknown"))
            End If
            ' next search
            Console.Out.Write("Search term (press Enter to stop): ")
            searchedName = Console.ReadLine().Trim()
        End While
    End Sub
End Module

The execution results are as follows:

The dictionary has 4 elements
[List of keys]
melanie
paul
violette
jean
[List of values]
10
18
15
20
[List of keys & values]
key=melanie value=10
key=paul value=18
key=violette value=15
key=jean value=20
[Deleting a key]
[List of keys & values]
key=melanie value=10
key=violet value=15
key=jean value=20
Name searched (nothing to stop): paul
Name paul unknown
Name searched (nothing to stop): melanie
melanie,10
Name searched (nothing to stop):

The program also uses an IEnumerator object to iterate through the collections of keys and values in the dictionary of type ICollection (see the Keys and Values properties above). A collection is a set of objects that can be iterated over. The ICollection interface is defined as follows:

Image

The Count property allows us to determine the number of elements in the collection. The ICollection interface derives from the IEnumerable interface:

Image

This interface has only one method, GetEnumerator, which allows us to obtain an object of type IEnumerator:

Image

The GetEnumerator() method of an ICollection allows us to iterate through the collection using the following methods:

MoveNext
moves to the next element in the collection. Returns true if this element exists, false otherwise. The first MoveNext moves to the first element. The "current" element of the collection is then available in the Current property of the enumerator
Current
property: current element of the collection
Reset
resets the iterator to the beginning of the collection, i.e., before the first element.

The structure for iterating over the elements of a C collection (ICollection) is therefore as follows:

' define the collection
dim C as ICollection C=...
' obtain an enumerator for this collection
dim iterator as IEnumerator = C.GetEnumerator();
' iterate through the collection using this enumerator
while(iterator.MoveNext())
    ' we have a current element
    ' use iterator.Current
end while

4.7. The StreamReader class

The StreamReader class allows you to read the contents of a file. Here are some of its properties and methods:

Public Sub New(ByVal path As String)
opens a stream from the file path. An exception is thrown if the file does not exist
Overrides Public Sub Close()
closes the stream
Overrides Public Function ReadLine() As String
Reads a line from the open stream
Overrides Public Function ReadToEnd() As String
reads the rest of the stream from the current position

Here is an example:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Collections
Imports System.IO

Module test
    Sub Main()
        Dim line As String = Nothing
        Dim infoStream As StreamReader = Nothing
        ' Read file contents
        Try
            infoStream = New StreamReader("infos.txt")
            line = infoStream.ReadLine()
            While Not (line Is Nothing)
                System.Console.Out.WriteLine(line)
                line = infoStream.ReadLine()
            End While
        Catch e As Exception
            System.Console.Error.WriteLine("The following error occurred: " & e.ToString)
        Finally
            Try
                infoStream.Close()
            Catch
            End Try
        End Try
    End Sub
End Module

and its execution results:

dos>more info.txt
12620:0:0
13190:0,05:631
15640:0,1:1290.5
24740:0.15:2072.5
31810:0.2:3309.5
39970:0.25:4900
48,360:0.3:6,898.5
55790:0.35:9316.5
92970:0.4:12106
127860:0.45:16754.5
151250:0.5:23147.5
172040:0.55:30710
195000:0.6:39312
0:0.65:49062

back>file1
12620:0:0
13190:0.05:631
15640:0.1:1290.5
24740:0.15:2072.5
31810:0.2:3309.5
39970:0.25:4900
48,360:0.3:6,898.5
55790:0.35:9316.5
92970:0.4:12106
127860:0.45:16754.5
151250:0.5:23147.5
172040:0.55:30710
195000:0.6:39312
0:0.65:49062

4.8. The StreamWriter Class

The StreamWriter class allows you to write to a file. Here are some of its properties and methods:

Public Sub New(ByVal path As String)
opens a write stream from the file path. An exception is thrown if the stream cannot be created
Public Overridable Property AutoFlush
As Boolean
If set to true, writing to the stream bypasses the buffer; otherwise, writing to the stream is not immediate: data is first written to a buffer and then to the stream when the buffer is full. By default, the buffered mode is used. It is well-suited for file streams but generally not for network streams.
Public Overridable Property NewLine
As String
To set or retrieve the line-ending character used by the WriteLine method
Overrides Public Sub Close()
Closes the stream
Overloads Public Overridable Sub
WriteLine(ByVal value As String)
writes a line to the write stream
Overrides Public Sub Flush()
flushes the buffer to the stream

Consider the following example:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Collections
Imports System.IO

Module test
    Sub Main()
        Dim line As String = Nothing        ' a line of text
        Dim fluxInfos As StreamWriter = Nothing        ' the text file
        Try
            ' create the text file
            infoStream = New StreamWriter("info.txt")
            ' read line typed on the keyboard
            Console.Out.Write("line (press any key to stop): ")
            line = Console.In.ReadLine().Trim()
            ' loop while the entered line is not empty
            While line <> ""
                ' Write line to text file
                infoStream.WriteLine(line)
                ' read a new line from the keyboard
                Console.Out.Write("line (press any key to stop): ")
                line = Console.In.ReadLine().Trim()
            End While
        Catch e As Exception
            System.Console.Error.WriteLine("The following error occurred: " & e.ToString)
        Finally
            ' Close file
            Try
                infoStream.Close()
            Catch
            End Try
        End Try
    End Sub
End Module

and the execution results:

dos>file2
line (nothing to stop it): line1
line (nothing to stop): line2
line (nothing to stop): line3
line (nothing to stop):

dos>more info.txt
line1
line2
line3

4.9. The Regex Class

The Regex class allows the use of regular expressions. These are used to validate the format of a string. For example, you can verify that a string representing a date is in the dd/mm/yy format. To do this, you use a pattern and compare the string to that pattern. In this example, d, m, and y must be digits. The pattern for a valid date format is therefore "\d\d/\d\d/\d\d", where the symbol \d represents a digit. The symbols that can be used in a pattern are as follows (Microsoft documentation):

 
Description
\
Designates the following character as a special character or literal. For example, "n" corresponds to the character "n". "\n" corresponds to a newline character. The sequence "\\" corresponds to "\", while "\(" corresponds to "(".
^
Matches the start of the input.
$
Matches the end of the input.
*
Matches the preceding character zero or more times. Thus, "zo*" matches "z" or "zoo".
+
Matches the preceding character one or more times. Thus, "zo+" matches "zoo", but not "z".
?
Matches the preceding character zero or one time. For example, "a?ve?" matches "ve" in "lever".
.
Matches any single character, except the newline character.

(pattern)
Searches for the pattern and stores the match. The matching substring can be retrieved from the resulting Matches collection using Item [0]...[n]. To match characters enclosed in parentheses ( ), use "\(" or "\)".
x|y
Matches either x or y. For example, "z|foot" matches "z" or "foot". "(z|f)oo" matches "zoo" or "foo".

{n}
n is a non-negative integer. Matches exactly n occurrences of the character. For example, "o{2}" does not match "o" in "Bob," but matches the first two "o"s in "fooooot".

{n,}
n is a non-negative integer. Matches at least n occurrences of the character. For example, "o{2,}" does not match "o" in "Bob," but matches all "o"s in "fooooot." "o{1,}" is equivalent to "o+" and "o{0,}" is equivalent to "o*."

{n,m}
m and n are non-negative integers. Matches at least n and at most m occurrences of the character. For example, "o{1,3}" matches the first three "o"s in "foooooot" and "o{0,1}" is equivalent to "o?".

[xyz]
Character set. Matches any of the specified characters. For example, "[abc]" matches "a" in "plat".

[^xyz]
Negative character set. Matches any character not listed. For example, "[^abc]" matches "p" in "plat".

[a-z]
Character range. Matches any character in the specified range. For example, "[a-z]" matches any lowercase alphabetical character between "a" and "z".

[^m-z]
Negative character range. Matches any character not in the specified range. For example, "[^m-z]" matches any character not between "m" and "z".
\b
Matches a word boundary, that is, the position between a word and a space. For example, "er\b" matches "er" in "lever," but not "er" in "verb."
\B
Matches a boundary that does not represent a word. "en*t\B" matches "ent" in "bien entendu".
\d
Matches a character representing a digit. Equivalent to [0-9].
\D
Matches a character that is not a digit. Equivalent to [^0-9].
\f
Matches a line break character.
\n
Matches a newline character.
\r
Equivalent to a carriage return character.
\s
Matches any whitespace, including space, tab, page break, etc. Equivalent to "[ \f\n\r\t\v]".
\S
Matches any non-whitespace character. Equivalent to "[^ \f\n\r\t\v]".
\t
Matches a tab character.
\v
Matches a vertical tab character.
\w
Matches any character representing a word and including an underscore. Equivalent to "[A-Za-z0-9_]".
\W
Matches any character that does not represent a word. Equivalent to "[^A-Za-z0-9_]".

\num
Matches num, where num is a positive integer. Refers to stored matches. For example, "(.)\1" matches two consecutive identical characters.

\n
Matches n, where n is an octal escape value. Octal escape values must consist of 1, 2, or 3 digits. For example, "\11" and "\011" both match a tab character. "\0011" is equivalent to "\001" & "1". Octal escape values must not exceed 256. If they do, only the first two digits are taken into account in the expression. Allows ASCII codes to be used in regular expressions.

\xn
Corresponds to n, where n is a hexadecimal escape value. Hexadecimal escape values must consist of two digits. For example, "\x41" corresponds to "A". "\x041" is equivalent to "\x04" & "1". Allows the use of ASCII codes in regular expressions.

An element in a pattern may appear once or multiple times. Let’s look at a few examples involving the \d symbol, which represents a single digit:

pattern
meaning
\d
a digit
\d?
0 or 1 digit
\d*
0 or more digits
\d+
1 or more digits
\d{2}
2 digits
\d{3,}
at least 3 digits
\d{5,7}
between 5 and 7 digits

Now let’s imagine a model capable of describing the expected format for a string:

target string
pattern
a date in dd/mm/yy format
\d{2}/\d{2}/\d{2}
a time in hh:mm:ss format
\d{2}:\d{2}:\d{2}
an unsigned integer
\d+
a sequence of spaces, which may be empty
\s*
an unsigned integer that may be preceded or followed by spaces
\s*\d+\s*
an integer that may be signed and preceded or followed by spaces
\s*[+|-]?\s*\d+\s*
an unsigned real number that may be preceded or followed by spaces
\s*\d+(.\d*)?\s*
a real number that may be signed and preceded or followed by spaces
\s*[+|]?\s*\d+(.\d*)?\s*
a string containing the word "just"
\bjuste\b
  

You can specify where to search for the pattern in the string:

pattern
meaning
^pattern
the pattern starts the string
pattern$
the pattern ends the string
^pattern$
the pattern starts and ends the string
pattern
the pattern is searched for anywhere in the string, starting from the beginning.
search string
pattern
a string ending with an exclamation point
!$
a string ending with a period
\.$
a string beginning with the sequence //
^//
a string consisting of a single word, optionally followed or preceded by spaces
^\s*\w+\s*$
a string consisting of two words, optionally followed or preceded by spaces
^\s*\w+\s*\w+\s*$
a string containing the word secret
\bsecret\b

Sub-patterns of a pattern can be "extracted." Thus, not only can we verify that a string matches a particular pattern, but we can also extract from that string the elements corresponding to the sub-patterns of the pattern that have been enclosed in parentheses. For example, if we are parsing a string containing a date in the format dd/mm/yy and we also want to extract the elements dd, mm, and yy from that date, we would use the pattern (\d\d)/(\d\d)/(\d\d).

4.9.1. Checking if a string matches a given pattern

A Regex object is constructed as follows:

Public Sub New(ByVal pattern As String)

The constructor creates a "regular expression" object from a pattern passed as a parameter (pattern). Once the regular expression pattern is constructed, it can be compared to character strings using the IsMatch method:

Overloads Public Function IsMatch(ByVal input As String) As Boolean

IsMatch returns true if the input string matches the regular expression pattern. Here is an example:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Collections
Imports System.Text.RegularExpressions

Module regex1

    Sub Main()
        ' a regular expression pattern
        Dim pattern1 As String = "^\s*\d+\s*$"
        Dim regex1 As New Regex(pattern1)
        ' compare a sample to the pattern
        Dim example1 As String = "  123  "
        If regex1.IsMatch(copy1) Then
            display(("[" + example1 + "] matches the pattern [" + pattern1 + "]"))
        Else
            display(("[" + example1 + "] does not match the pattern [" + pattern1 + "]"))
        End If
        Dim example2 As String = "  123a  "
        If regex1.IsMatch(copy2) Then
            display(("[" + copy2 + "] matches the pattern [" + pattern1 + "]"))
        Else
            display(("[" + copy2 + "] does not match the pattern [" + pattern1 + "]"))
        End If
    End Sub

    ' display 
    Sub display(ByVal msg As String)
        Console.Out.WriteLine(msg)
    End Sub
End Module

and the execution results:

dos>vbc /r:system.dll regex1.vb
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4 for Microsoft (R) .NET Framework version 1.1.4322.573

dos>regex1
[  123  ] matches the pattern [^\s*\d+\s*$]
[  123a  ] does not match the pattern [^\s*\d+\s*$]

4.9.2. Find all elements in a string that match a pattern

The Matches method

Overloads Public Function Matches(ByVal input As String) As MatchCollection

returns a collection of elements from the input string that match the pattern, as shown in the following example:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Collections
Imports System.Text.RegularExpressions

Module regex2
    Sub Main()
        ' multiple occurrences of the pattern in the string
        Dim pattern2 As String = "\d+"
        Dim regex2 As New Regex(pattern2)        '
        Dim instance3 As String = "  123  456  789 "
        Dim results As MatchCollection = regex2.Matches(instance3)
        display(("Pattern=[" + pattern2 + "],text=[" + text3 + "]"))
        display(("There are " & results.Count & " occurrences of the pattern in the sample "))
        Dim i As Integer
        For i = 0 To results.Count - 1
            display((results(i).Value & " at position " & results(i).Index))
        Next i
    End Sub

    'display
    Sub display(ByVal msg As String)
        Console.Out.WriteLine(msg)
    End Sub
End Module

The MatchCollection class has a Count property, which is the number of elements in the collection. If results is a MatchCollection object, results(i) is the i-th element of this collection and is of type Match. In our example, results is the set of elements in the string example3 that match the pattern pattern2, and results(i) is one of these elements. The Match class has two properties that are relevant here:

  • Value: the value of the Match object, i.e., the element matching the pattern
  • Index: the position where the element was found in the searched string

The results of running the previous program:

dos>vbc /r:system.dll regex2.vb
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4p for Microsoft (R) .NET Framework version 1.1.4322.573

dos>regex2
Pattern=[\d+],match=[  123  456  789 ]
There are 3 occurrences of the pattern in the sample
123 at position 2
456 at position 7
789 at position 12

4.9.3. Extracting parts of a pattern

Sub-sets of a pattern can be "extracted." Thus, not only can we verify that a string matches a particular pattern, but we can also extract from that string the elements corresponding to the pattern’s sub-sets that have been enclosed in parentheses. So, if we are parsing a string containing a date in the format dd/mm/yy and we also want to extract the elements dd, mm, and yy from that date, we will use the pattern (\d\d)/(\d\d)/(\d\d). Let’s examine the following example:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Collections
Imports System.Text.RegularExpressions

Module regex2
    Sub Main()
        ' capture elements in the pattern
        Dim pattern3 As String = "(\d\d):(\d\d):(\d\d)"
        Dim regex3 As New Regex(pattern3)
        Dim example4 As String = "It is 18:05:49"
        ' check pattern
        Dim result As Match = regex3.Match(example4)
        If result.Success Then
            ' the sample matches the pattern
            display(("The sample [" + sample4 + "] matches the pattern [" + pattern3 + "]"))
            ' display the groups
            Dim i As Integer
            For i = 0 To result.Groups.Count - 1
                display(("group[" & i & "]=[" & result.Groups(i).Value & "] at position " & result.Groups(i).Index))
            Next i
        Otherwise
            ' The instance does not match the template
            display(("The record [" + record4 + " does not match the template [" + template3 + "]"))
        End If
    End Sub

    'display
    Sub display(ByVal msg As String)
        Console.Out.WriteLine(msg)
    End Sub
End Module

Running this program produces the following results:

dos>vbc /r:system.dll regex3.vb
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4

dos>regex3
The instance [It is 18:05:49] matches the pattern [(\d\d):(\d\d):(\d\d)]
groups[0]=[18:05:49] at position 7
groups[1]=[18] at position 7
groups[2]=[05] at position 10
groups[3]=[49] at position 13

The new feature is found in the following code snippet:


        ' pattern check
        Dim result As Match = regex3.Match(example4)
        If result.Success Then
            ' the instance matches the pattern
            display(("The instance [" + instance4 + "] matches the pattern [" + pattern3 + "]"))
            ' display the groups
            Dim i As Integer
            For i = 0 To result.Groups.Count - 1
                display(("group[" & i & "]=[" & result.Groups(i).Value & "] at position " & result.Groups(i).Index))
            Next i
        Else

The string example4 is compared to the regex3 pattern using the Match method. This returns a Match object, as previously described. Here, we use two new properties of this class:

  • Success: indicates whether there was a match
  • Groups: a collection where
    • Groups[0] corresponds to the part of the string matching the pattern
    • Groups[i] (i>=1) corresponds to the i-th group of parentheses

If the result is of type Match, results.Groups is of type GroupCollection and results.Groups[i] is of type Group. The Group class has two properties that we use here:

  • Value: the value of the Group object containing the element corresponding to the content of a parenthesis
  • Index: the position where the element was found in the parsed string

4.9.4. A practice program

Finding the regular expression that allows us to verify that a string matches a certain pattern can sometimes be a real challenge. The following program allows you to practice. It asks for a pattern and a string and then indicates whether or not the string matches the pattern.


' options
Option Strict On
Option Explicit On 

' Namespaces
Imports System
Imports System.Collections
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic

Module regex4
    Sub Main()
        ' a regular expression pattern
        Dim pattern, string As String
        Dim regex As Regex = Nothing
        Dim results As MatchCollection
        ' Ask the user for the patterns and instances to compare against it
        Dim finished1 As Boolean = False
        Do While Not finished1
            Dim error As Boolean = True
            Do While Not finished1 And error
                ' Ask for the template
                Console.Out.Write("Enter the pattern to test or 'end' to stop:")
                pattern = Console.In.ReadLine()
                ' Done?
                If pattern.Trim().ToLower() = "end" Then
                    completed1 = True
                Else
                    ' create the regular expression
                    Try
                        regex = New Regex(pattern)
                        error = False
                    Catch ex As Exception
                        Console.Error.WriteLine(("Error: " + ex.Message))
                    End Try
                End If
            Loop
            ' Done?
            If finished1 Then Exit Sub
            ' Ask the user which copies to compare to the template
            Dim finished2 As Boolean = False
            Do While Not finished2
                Console.Out.Write(("Enter the string to compare to the template [" + template + "] or 'end' to stop:"))
                string = Console.In.ReadLine()
                ' Done?
                If string.Trim().ToLower() = "end" Then
                    finished2 = True
                Else
                    ' Perform the comparison
                    results = regex.Matches(string)
                    ' success?
                    If results.Count = 0 Then
                        Console.Out.WriteLine("No matches found")
                    Else
                        ' display the elements matching the pattern
                        Dim i As Integer
                        For i = 0 To results.Count - 1
                            Console.Out.WriteLine(("I found the match [" & results(i).Value & "] at position " & results(i).Index))
                            ' of subelements
                            If results(i).Groups.Count <> 1 Then
                                Dim j As Integer
                                For j = 1 To (results(i).Groups.Count) - 1
                                    Console.Out.WriteLine((ControlChars.Tab & "sub-element [" & results(i).Groups(j).Value & "] at position " & results(i).Groups(j).Index))
                                Next j
                            End If
                        Next i
                    End If
                End If
            Loop
        Loop
    End Sub

    'display
    Sub display(ByVal msg As String)
        Console.Out.WriteLine(msg)
    End Sub
End Module

Here is an example of execution:

Type the pattern to test or "end" to stop:\d+
Type the string to compare with the pattern [\d+] or "end" to stop:123 456 789
I found match [123] in position 0
I found the match [456] in position 4
I found a match [789] at position 8
Type the string to compare against the pattern [\d+] or "fin" to stop :fin

Type the pattern to test or "end" to stop: (\d\d):(\d\d)
Type the string to compare against the pattern [(\d\d):(\d\d)] or "end" to stop: 14:15 abcd 17:18 xyzt
I found the match [14:15] at position 0
        subelement [14] at position 0
        subelement [15] at position 3
I found the match [17:18] at position 11
        subelement [17] at position 11
        subelement [18] at position 14
Type the string to compare against the pattern [(\d\d):(\d\d)] or "end" to stop :end

Type the pattern to test or "end" to stop :^\s*\d+\s*$
Type the string to compare against the pattern [^\s*\d+\s*$] or "end" to stop:  1456
I found a match [  1456] at position 0
Type the string to compare against the pattern [^\s*\d+\s*$] or "end" to stop: end

Type the pattern to test or "end" to stop: \/s*(\d+)\s*$
Type the string to match against the pattern [^\s*(\d+)\s*$] or "end" to stop: 1456
I found the match [1456] at position 0
        subelement [1456] at position 0
Type the string to compare against the pattern [^\s*(\d+)\s*$] or "end" to stop: abcd 1
456
No matches found
Type the string to compare against the pattern [^\s*(\d+)\s*$] or "end" to stop: end

Type the pattern to test or "end" to stop: end

4.9.5. The Split method

We have already encountered this method in the String class:

Overloads Public Function Split(ByVal ParamArray separator() As Char) As String()

The string is treated as a sequence of fields separated by the characters in the [separator] array. The result is an array of these fields.

The field separator in the string is one of the characters in the separator array. The Split method of the Regex class allows us to specify the separator based on a pattern:

Overloads Public Function Split(ByVal input As String) As String()

The string [input] is split into fields, which are separated by a separator matching the pattern of the current Regex object. Suppose, for example, that we have lines in a text file of the form field1, field2, ..., fieldn. The fields are separated by a comma, but this may be preceded or followed by spaces. The Split method of the String class is therefore not suitable. The RegEx method provides the solution. If line is the line read, the fields can be obtained by

dim fields() as string = new Regex("s*,\s*").Split(line)

as shown in the following example:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Text.RegularExpressions

Module regex5
    Sub Main()
        ' a line
        Dim line As String = "abc, def, ghi"
        ' a pattern
        Dim pattern As New Regex("\s*,\s*")
        ' split line into fields
        Dim fields As String() = pattern.Split(line)
        ' display
        Dim i As Integer
        For i = 0 To champs.Length - 1
            Console.Out.WriteLine(("fields[" & i & "]=[" & fields(i) & "]"))
        Next i
    End Sub
End Module

Execution results:

dos>vbc /r:system.dll regex5.vb
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4

dos>regex5
fields[0] = [abc]
fields[1] = [def]
fields[2] = [ghi]

4.10. The BinaryReader and BinaryWriter Classes

The BinaryReader and BinaryWriter classes are used to read and write binary files. Consider the following application. We want to write a program that would be called as follows:

// syntax pg text bin
// we read a text file (text) and store its contents in a
// binary file
// the text file contains lines of the form name : age
// which we will store in a string, int structure

The text file has the following content:

paul: 10
helene : 15
Jacques: 11
sylvain: 12

The program is as follows:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Text.RegularExpressions
Imports System.IO

' BinaryWriter()

Module bw1
    ' syntax: pg text bin
    ' Read a text file and store its contents in a
    ' binary file
    ' the text file contains lines in the form name : age
    ' which we will store in a structure string, int

    Sub Main(ByVal arguments() As String)
        ' Two arguments are required
        Dim nbArgs As Integer = arguments.Length
        If nbArgs <> 2 Then
            Console.Error.WriteLine("syntax: pg binary text")
            Environment.Exit(1)
        End If
        ' Open the text file for reading
        Dim input As StreamReader = Nothing
        Try
            input = New StreamReader(arguments(0))
        Catch ex As Exception
            Console.Error.WriteLine("Error opening file " & arguments(0) & "(" & ex.Message & ")")
            Environment.Exit(2)
        End Try
        ' Open the binary file for writing
        Dim output As BinaryWriter = Nothing
        Try
            output = New BinaryWriter(New FileStream(arguments(1), FileMode.Create, FileAccess.Write))
        Catch ex As Exception
            Console.Error.WriteLine("Error opening file " & arguments(1) & "(" & ex.Message & ")")
            Environment.Exit(3)
        End Try
        ' Read text file - Write binary file
        ' line from text file
        Dim line As String
        ' field separator in the line
        Dim separator As New Regex("\s*:\s*")
        ' age pattern
        Dim agePattern As New Regex("\s*\d+\s*")
        Dim lineNumber As Integer = 0
        Dim processingDone As Boolean
        Dim fields As String()
        line = input.ReadLine()
        While Not (line Is Nothing)
            processingDone = False
            ' Is the line empty?
            If line.Trim() = "" Then
                processingDone = True
            End If
            ' one more line
            If Not processingDone Then
                lineNumber += 1
                ' one more line: name : age
                fields = separator.Split(line)
                ' we need 2 fields
                If fields.Length <> 2 Then
                    Console.Error.WriteLine(("Line " & numLine & " of file " & arguments(0) & " has an incorrect number of fields"))
                    ' next line
                    processingDone = True
                End If
            End If
            If Not processingDone Then
                ' the second field must be an integer >= 0
                If Not modAge.IsMatch(fields(1)) Then
                    Console.Error.WriteLine(("Line " & lineNumber & " of file " & arguments(0) & " has an incorrect age"))
                    ' next line
                    processingDone = True
                End If
                ' Write the data to the binary file
                output.Write(fields(0))
                output.Write(Integer.Parse(fields(1)))
            End If
            'next line
            line = input.ReadLine()
        End While
        ' Close files
        input.Close()
        output.Close()
    End Sub
End Module

Let's take a closer look at the operations related to the BinaryWriter class:

  • The BinaryWriter object is opened by the operation

            output = New BinaryWriter(New FileStream(arguments(1), FileMode.Create, FileAccess.Write))

The constructor's argument must be a stream (Stream). Here, it is a stream created from a file (FileStream) for which we specify:

  • (continued)
    • the name
    • the operation to perform, here `FileMode.Create` to create the file
    • the access type, here FileAccess.Write for write access to the file
  • the write operation

                ' we write the data to the binary file
                output.Write(fields(0))
                output.Write(Integer.Parse(fields(1)))

The BinaryWriter class has various overloaded Write methods for writing different types of simple data

  • the stream closing operation
        output.Close()

The results of the previous execution will be provided by the following program. This program also accepts two arguments:

    ' syntax pg bin text
    ' we read a binary file bin and store its contents in a text file (text)
    ' the binary file has a structure of type string, int
    ' the text file has lines of the form name : age

We are therefore performing the reverse operation. We read a binary file to create a text file. If the resulting text file is identical to the original file, we will know that the text --> binary --> text conversion was successful. The code is as follows:


' options
Option Strict On
Option Explicit On 

' namespaces
Imports System
Imports System.Text.RegularExpressions
Imports System.IO

Module br1
    ' syntax: pg bin text
    ' read a binary file bin and store its contents in a text file (text)
    ' the binary file has a structure of string, int
    ' the text file contains lines in the form name : age

    Sub Main(ByVal arguments() As String)
        ' requires 2 arguments
        Dim nbArgs As Integer = arguments.Length
        If nbArgs ≠ 2 Then
            Console.Error.WriteLine("syntax: pg binary text")
            Environment.Exit(1)
        End If
        ' Open the binary file for reading
        Dim dataIn As BinaryReader = Nothing
        Try
            dataIn = New BinaryReader(New FileStream(arguments(0), FileMode.Open, FileAccess.Read))
        Catch ex As Exception
            Console.Error.WriteLine("Error opening file " & arguments(0) & "(" & ex.Message & ")")
            Environment.Exit(2)
        End Try
        ' Open the text file for writing
        Dim dataOut As StreamWriter = Nothing
        Try
            dataOut = New StreamWriter(arguments(1))
            dataOut.AutoFlush = True
        Catch ex As Exception
            Console.Error.WriteLine("Error opening file " & arguments(1) & "(" & ex.Message & ")")
            Environment.Exit(3)
        End Try
        ' Read binary file - Write text file
        Dim name As String        ' person's name
        Dim age As Integer        ' their age
        ' loop to process the binary file
        While True
            ' read name
            Try
                name = dataIn.ReadString()
            Catch
                ' end of file
                Exit Sub
            End Try
            ' read age
            Try
                age = dataIn.ReadInt32()
            Catch
                Console.Error.WriteLine("The file " & arguments(0) + " does not appear to be in the correct format")
                Exit Sub
            End Try
            ' writing to text file
            dataOut.WriteLine(name & ":" & age)
            System.Console.WriteLine(name & ":" & age)
        End While
        ' Close everything
        dataIn.Close()
        dataOut.Close()
    End Sub
End Module

Let's take a closer look at the operations related to the BinaryReader class:

  • The BinaryReader object is opened by the operation

            dataIn = New BinaryReader(New FileStream(arguments(0), FileMode.Open, FileAccess.Read))

The constructor's argument must be a stream (Stream). Here, it is a stream created from a file (FileStream) for which we specify:

  • (continued)
    • the name
    • the operation to perform, here `FileMode.Open` to open an existing file
    • the access type, here FileAccess.Read for read access to the file
  • the read operation
                name = dataIn.ReadString()
                age = dataIn.ReadInt32()

The BinaryReader class provides various ReadXX methods for reading different types of simple data

  • the operation to close the stream
        dataIn.Close()

If we run the two programs in sequence, converting personnes.txt to personnes.bin and then personnes.bin to personnes.txt2, we get:

dos>more people.txt
paul : 10
helene : 15
jacques : 11
sylvain: 12

dos>more people.txt2
Paul: 10
Helene: 15
Jacques: 11
sylvain:12

dos>dir
04/29/2002  6:19 PM                   54 people.txt
04/29/2002  6:19 PM                   44 people.bin
04/29/2002  6:20 PM                   44 people.txt2