Exercise
Table + array + files
Objetive
Expand the exercise (tables + array) by adding two new methods to dump the data of the array into a binary file and restore the data from the file.
Code
Imports System
Imports System.IO
Namespace Tables
Class Table
Protected width, height As Integer
Public Sub New(ByVal tableWidth As Integer, ByVal tableHeight As Integer)
width = tableWidth
height = tableHeight
End Sub
Public Sub ShowData()
Console.WriteLine("Width: {0}, Height: {1}", width, height)
End Sub
Public Sub Save(ByVal name As String)
Dim outputFile As BinaryWriter = New BinaryWriter(File.Open(name, FileMode.Create))
outputFile.Write(height)
outputFile.Write(width)
outputFile.Close()
End Sub
Public Sub Load(ByVal name As String)
Dim inputFile As BinaryReader = New BinaryReader(File.Open(name, FileMode.Open))
height = inputFile.ReadInt32()
width = inputFile.ReadInt32()
inputFile.Close()
End Sub
End Class
End Namespace
Namespace Tables
Class TestTable
Private Shared Sub Main(ByVal args As String())
Dim tableList As Table() = New Table(9) {}
Dim random As Random = New Random()
For i As Integer = 0 To tableList.Length - 1 - 1
tableList(i) = New Table(random.[Next](50, 201), random.[Next](50, 201))
Next
tableList(0).Save("1.dat")
tableList(9) = New Table(0, 0)
tableList(9).Load("1.dat")
For i As Integer = 0 To tableList.Length - 1
tableList(i).ShowData()
Next
Console.ReadLine()
End Sub
End Class
End Namespace