Results 1 to 3 of 3

Thread: Save and Load Array Structure Easy

  1. #1
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 10
    Location
    Wales UK
    Posts
    540

    Save and Load Array Structure Easy

    Hi this is a very simple example of how you can save arrays of Structure to files you remember how easy this was in VB6 well it almost the same. This example just shows how to save data using a basic person example.

    Ok start a new forms project add two commend buttons

    Button1
    Name : cmdSave
    Text : Save

    Button2
    Name : cmdLoad
    Text : Load

    Next add the code, then hit the load button.
    This will save a small data example button load will display persons name in message box. Hope you find it useful comments suggestions are welcome.

    Code

    vbnet Code:
    1. Option Explicit On
    2.  
    3. Public Class Form1
    4.  
    5.     Private Structure TPerson
    6.         Dim Id As Integer
    7.         Dim Name As String
    8.         Dim Age As Integer
    9.     End Structure
    10.  
    11.     Private Persons() As TPerson
    12.     Private PersonCount As Integer
    13.  
    14.     Private Sub Reset()
    15.         'Reset person count and array
    16.         PersonCount = 0
    17.         Erase Persons
    18.     End Sub
    19.  
    20.     Private Sub AddPerson(ByVal ID As Integer, ByVal Name As String, ByVal Age As Integer)
    21.         ReDim Preserve Persons(PersonCount)
    22.         'Add person to persons
    23.         With Persons(PersonCount)
    24.             .Id = ID
    25.             .Name = Name
    26.             .Age = Age
    27.         End With
    28.         'INC Counter
    29.         PersonCount += 1
    30.     End Sub
    31.  
    32.     Private Function LoadFromFile(ByVal Source As String) As Boolean
    33.         Dim fp As Integer = FreeFile()
    34.         Dim sig(2) As Char
    35.  
    36.         FileOpen(fp, Source, OpenMode.Binary, OpenAccess.Read)
    37.         'Get header
    38.         FileGet(fp, sig(0))
    39.         FileGet(fp, sig(1))
    40.         FileGet(fp, sig(2))
    41.  
    42.         If (New String(sig)) <> "BAG" Then
    43.             Return False
    44.         Else
    45.             'Get person count
    46.             FileGet(fp, PersonCount)
    47.             'Get person records.
    48.             ReDim Preserve Persons(PersonCount)
    49.             FileGet(fp, Persons)
    50.             FileClose(fp)
    51.             'Return good value
    52.             Return True
    53.         End If
    54.  
    55.     End Function
    56.  
    57.     Private Sub WriteToFile(ByVal Source As String)
    58.         Dim fp As Integer = FreeFile()
    59.         Dim sig(2) As Char
    60.  
    61.         'Setup header
    62.         sig(0) = "B"
    63.         sig(1) = "A"
    64.         sig(2) = "G"
    65.  
    66.         FileOpen(fp, Source, OpenMode.Binary, OpenAccess.Write)
    67.         FilePut(fp, sig(0))
    68.         FilePut(fp, sig(1))
    69.         FilePut(fp, sig(2))
    70.         FilePut(fp, Persons.Length - 1)
    71.         FilePut(fp, Persons)
    72.         FileClose(fp)
    73.  
    74.     End Sub
    75.  
    76.     Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    77.         'Add some random people.
    78.         AddPerson(0, "Ben", 35)
    79.         AddPerson(1, "Joe", 25)
    80.         AddPerson(2, "Gemma", 32)
    81.         AddPerson(3, "Bill", 56)
    82.         AddPerson(4, "Jack", 24)
    83.         'Write to filename.
    84.         WriteToFile("C:\out\data\data.txt")
    85.         'Reset
    86.         Reset()
    87.     End Sub
    88.  
    89.     Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
    90.         'Load persons data.
    91.         If LoadFromFile("C:\out\data\data.txt") <> True Then
    92.             'Show message message.
    93.             MessageBox.Show("Error loading data", "Error", MessageBoxButtons.OK,
    94.                             MessageBoxIcon.Information)
    95.         Else
    96.             'Show persons
    97.             For x As Integer = 0 To PersonCount
    98.                 MessageBox.Show(Persons(x).Name, "", MessageBoxButtons.OK,
    99.                                 MessageBoxIcon.Information)
    100.             Next
    101.             'Reset
    102.             Reset()
    103.         End If
    104.     End Sub
    105. End Class

  2. #2
    Oranges!
    Join Date
    May 08
    Location
    British Columbia, Canada
    Posts
    2,781

    Re: Save and Load Array Structure Easy

    You really, really, really shouldn't use the VB6-style FileXXX methods. Instead, use the classes in System.IO. Plenty of good resources out there.

  3. #3
    Lively Member
    Join Date
    Jan 11
    Posts
    107

    Re: Save and Load Array Structure Easy

    srry posted to wrong thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •