2

Here is my array:

    Public RacersArray(AmountOfRacers - 1) As Racer

<Serializable()> Public Structure Racer
    Public Name As String
    Public CleatSize As String
    Public SkillLevel As String
    Public Height As String
    Public Team As String
    Public CompatibilityArr() As String
End Structure

<Serializable()> Public Structure Compatibility
    Public Name As String
    Public Score As Integer
End Structure

Below is the code I am using to try save and load from file. The file is getting populated with what looks like the correct gibberish, but when loading the array and it's indexes are still 'nothing'

    Public Sub RacersInputSAVE()
    Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim fStream As New FileStream(SaveLocation, FileMode.Create)

    bf.Serialize(fStream, InputRacers.RacersArray) ' write to file

    fStream.Close()
End Sub

Public Sub RacersInputLOAD()
    Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim fStream As New FileStream(LoadLocation, FileMode.Open)

    InputRacers.RacersArray = bf.Deserialize(fStream) ' read from file

    fStream.Close()
End Sub
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Actually your code should work as is. Have you made sure that `SaveLocation` and `LoadLocation` are pointing to the same file? Perhaps having one `FileLocation` and passing that as a parameter to your subs would work better. Also consider trying a Using block, it's more intuitive. – tinstaafl Mar 08 '17 at 02:48

2 Answers2

1

For some reason, I have never had good luck serializing to a file using the BinaryFormatter. The exact reasons are lost to the mists of time, but I do know that SoapFormatter works correctly for me:

    Using oStream As Stream = File.Open(SaveLocation, FileMode.Create, IO.FileAccess.Write)
        If oStream IsNot Nothing Then
            Call (New System.Runtime.Serialization.Formatters.Soap.SoapFormatter).Serialize(oStream, InputRacers.RacersArray)
        End If
    End Using
competent_tech
  • 44,465
  • 11
  • 90
  • 113
1

The first thing that is probably wrong is this:

Public CompatibilityArr() As String

Based on the names it seems like that is supposed to be that second Compatibility structure. If so, it should be:

Public CompatibilityArr As Compatibility()

Its not clear if that is part of the problem being described (but when loading the array and it's indexes are still 'nothing' is vague since there is more than one array). Otherwise the second structure isnt being used.

Next, set Option Strict On. When deserializing, the BinaryFormatter always returns Object which needs to be cast to the correct Type:

Racers = DirectCast(bf.Deserialize(fs), Racer()) ' read from file

With those 2 changes, all the data made the round trip fine for me.

Third, Structure is not the right Type for that. Those should be classes, and rather than public Fields/Members, use Properties especially if there will be any data binding involved:

<Serializable()> 
Public Class Racer
    Public Property Name As String
    ...

Also, anything which implements a Dispose()method, like a FileStream should be used in a Using block which will close and dispose of the target object:

Dim bf As New BinaryFormatter
Using fs As New FileStream(fileName, FileMode.Create)
    bf.Serialize(fs, Racers) ' write to file
End Using

Finally, you would probably find some of the NET collections such as List(Of Racer) to be easier to work with than arrays. It takes about 15 mins to learn how they work.

MSDN: Choosing Between Class and Struct

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Thank you for all of the wonderful advice! Everything is working more than satisfactorily now, and I can't thank you enough. I'll be sure to brush up on my Lists in the future :) – Harry Allen Mar 08 '17 at 23:12
  • A list is very much like an array, but you dont have to know how big to make it - they grow automagically. For that: `Racers = DirectCast(bf.Deserialize(fs), List(Of Racer))` where racers is: `Dim Racers As List(Of Racer)` which is 85% of what you need to know about Lists – Ňɏssa Pøngjǣrdenlarp Mar 08 '17 at 23:13
  • See also **[Five Minute Intro to Classes and Lists](http://stackoverflow.com/a/34164458)** also added a link to a great article on MSDN about Classes vs Structures – Ňɏssa Pøngjǣrdenlarp Mar 08 '17 at 23:23