How do I read and write this array of structure to/from a file?

35 Views Asked by At

I'm trying to write the array persons to a file and read it and have no clue on how to go about it. Here's my code:

Public Class Form1
Structure Person
    Public name As String
    Public height As Integer
    Public weight As Double
End Structure
Dim persons(49) As Person
Dim arraySize As Integer = 0
Private Sub submitBtn_Click(sender As Object, e As EventArgs) Handles submitBtn.Click
    If arraySize < 50 Then
        Dim Name As String
        Dim Height As Integer
        Dim Weight As Double
        Name = nameTxt.Text
        Height = CInt(heightTxt.Text)
        Weight = CDbl(weightTxt.Text)
        nameTxt.Text = Nothing
        heightTxt.Text = Nothing
        weightTxt.Text = Nothing
        arraySize += 1
        persons(arraySize).name = Name
        persons(arraySize).height = Height
        persons(arraySize).weight = Weight
    Else
        MsgBox("The list of people is full now. You may no longer enter new people.")
    End If


End Sub
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
End Sub

Private Sub readBtn_Click(sender As Object, e As EventArgs) Handles readBtn.Click
End Sub

End Class

Any help on how to code this would be appreciated. Thank you!

I tried coding it to save the array persons (which is linked to the structure Person) to a file, but the app freezes, and i am not sure how to get around it.

1

There are 1 best solutions below

0
tuyau2poil On

try to use list :

Public Class Form1
<Serializable()> Structure Person
    Public name As String
    Public height As Integer
    Public weight As Double
End Structure

dim persons As List(Of Person)

Private Sub submitBtn_Click(sender As Object, e As EventArgs) Handles submitBtn.Click
    If persons.length < 50 Then
        Dim Name As String
        Dim Height As Integer
        Dim Weight As Double
        Name = nameTxt.Text
        Height = CInt(heightTxt.Text)
        Weight = CDbl(weightTxt.Text)
        nameTxt.Text = Nothing
        heightTxt.Text = Nothing
        weightTxt.Text = Nothing
        
        person.name = Name
        person.height = Height
        person.weight = Weight      
        persons.add(person) 
    Else
        MsgBox("The list of people is full now. You may no longer enter new people.")
    End If


End Sub

Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
        Using fs As New IO.FileStream("d:\backup\persons.dat", IO.FileMode.Create)
            Dim formatter As New BinaryFormatter
            formatter.Serialize(fs, persons)
        End Using
End Sub

Private Sub readBtn_Click(sender As Object, e As EventArgs) Handles readBtn.Click
         Using fs As New IO.FileStream("d:\backup\persons.dat", IO.FileMode.Open)
            Dim formatter As New BinaryFormatter
            persons = DirectCast(formatter.Deserialize(fs), List(Of person))
        End Using
End Sub

dont forget to add <Serializable()> in front of struc definition.