Ejercicio
Tabla + matriz + archivos
Objetivo
Expanda el ejercicio del 9 de enero (tablas + matriz), de modo que contenga dos nuevos métodos, volcar los datos de la matriz en un archivo binario y restaurar los datos del archivo.
Código
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