這裏是我的數組:試圖保存結構文件和加載的文件從一個數組(序列化,反序列化)
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
下面是我用的,試圖保存和加載從文件中的代碼。該文件得到填充的是一種看上去正確的廢話,而是裝載陣列,它的索引時仍「無」
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
其實你的代碼應工作原樣。你確定'SaveLocation'和'LoadLocation'指向同一個文件嗎?也許有一個'FileLocation'並將其作爲參數傳遞給你的潛艇會更好。另外考慮嘗試一個使用塊,它更直觀。 – tinstaafl