1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Text
Public Class Form1
Public MyArray(47, 0) As String
Private rnd As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n As Integer = rnd.Next(0, 20)
MyArray = New String(47, n) {}
FillArray()
DisplayArray(TextBox1)
Keep()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Rehydrate()
DisplayArray(TextBox2)
End Sub
Public Sub FillArray()
For i As Integer = 0 To MyArray.GetUpperBound(0)
For j As Integer = 0 To MyArray.GetUpperBound(1)
MyArray(i, j) = rnd.Next(0, 200)
Next
Next
End Sub
Public Sub Keep()
Dim olist As New ArrayList
olist.Add(MyArray)
Using fs As New FileStream(Application.StartupPath + "\Array.bin", FileMode.Create)
Dim b As New BinaryFormatter()
b.Serialize(fs, olist)
fs.Close()
End Using
End Sub
Public Sub Rehydrate()
Dim olist As ArrayList = Nothing
Using fs As New FileStream(Application.StartupPath + "\Array.bin", FileMode.Open)
Dim b As New BinaryFormatter()
olist = CType(b.Deserialize(fs), ArrayList)
fs.Close()
End Using
MyArray = CType(olist(0), String(,))
End Sub
Public Sub DisplayArray(ByVal tbox As TextBox)
Dim sb As New StringBuilder
tbox.Clear()
For i As Integer = 0 To MyArray.GetUpperBound(0)
For j As Integer = 0 To MyArray.GetUpperBound(1)
sb.Append(MyArray(i, j) + " ; ")
Next
sb.AppendLine()
Next
tbox.Text = sb.ToString
End Sub
End Class |
Partager