-1

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
    Do you have any requirements on what this file should be? Delimited text, XML, Json for example or some custom format maybe? There are literally hundreds of examples of this around. _I tried coding it_ Why not update your question with your best attempt and maybe someone can help diagnose and resolve the freezing issue. – Hursey Jan 31 '23 at 22:04
  • For starters, thank you for adding the code as requested previously. Do you have any requirements for what the file structure should use? commas separated values, json, xml? That was another part of the question. There are helpers for all of those options but you need to know what format you want the data in before you can output it. – ClearlyClueless Jan 31 '23 at 22:19
  • If you tried something and it didn't work, you need to show us what you did and explain exactly what happened. When you say that the app froze, do you just mean while the processing was being done or do you mean it locked up completely? If the former then that is not necessarily an issue with the code but simply that you're doing long-running work on the UI thread. If the latter then you need to debug the code to see what's actually happening, which will likely either be an infinite lop or a method call that never returns. – jmcilhinney Feb 01 '23 at 00:42

1 Answers1

0

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.

tuyau2poil
  • 767
  • 1
  • 2
  • 7