将项目插入列表会导致项目被删除

时间:2014-04-24 14:18:22

标签: vb.net list insert resize

我目前正在尝试将分数插入正确的位置。分数增加了他们的正确位置。 但是,当我添加或插入到包含的列表时,它不会扩展到其反序列化的大小。 (注意,在调用AddNewScore函数之后,这个高分类被序列化为XML。当程序加载时,它也被反序列化了)

问题仅在反序列化后发生。

这是我的序列化代码(它包含在管理保存和加载我使用的所有数据类型的序列化类中)

Public Sub LoadHighScores()
        _highScores = DirectCast(LoadXml(highScoreFilePath, highScoreBackupFilePath, _highScoreFileCorrupted, GetType(HighScores)), HighScores)
        If (_highScores.scores.Count < 5) Then
            _highScores.AddNewScore(New Score("Steve", 9000))
            _highScores.AddNewScore(New Score("John", 8000))
            _highScores.AddNewScore(New Score("Paul", 7000))
            _highScores.AddNewScore(New Score("Alex", 6000))
            _highScores.AddNewScore(New Score("Joe", 5000))
            SaveHighScores()
        End If
    End Sub


 Private Sub SaveHighScores()
        If (File.Exists(highScoreFilePath) And Not _highScoreFileCorrupted) Then
            File.Copy(highScoreFilePath, highScoreBackupFilePath, True) 'Backup quiz data
        End If
        Using FileStream As FileStream = New FileStream(highScoreFilePath, FileMode.Create)
            'Dim encryptionStream As CryptoStream = New CryptoStream(FileStream, AESCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
            Dim serializer As XmlSerializer = New XmlSerializer(GetType(HighScores))
            serializer.Serialize(FileStream, _highScores)
            _highScoreFileCorrupted = False
            'encryptionStream.Close()
        End Using
    End Sub

 Public Function LoadXml(ByVal filePath As String, ByVal backupFilePath As String, ByRef fileCorrupt As Boolean, ByVal dataType As Type) As Object
        Dim returnValue As Object = Activator.CreateInstance(dataType)
        If (File.Exists(filePath) Or File.Exists(backupFilePath)) Then
            Using fileStream As FileStream = New FileStream(filePath, FileMode.OpenOrCreate)
                Using backupFileStream As FileStream = New FileStream(backupFilePath, FileMode.OpenOrCreate)
                    Dim serializer As XmlSerializer = New XmlSerializer(dataType)
                    Try
                        returnValue = serializer.Deserialize(fileStream)
                    Catch ex As Exception
                        fileCorrupt = True
                        Try
                            returnValue = serializer.Deserialize(backupFileStream)
                        Catch e As Exception
                            MessageBox.Show("Backup Corrupted", "Loading error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End Try
                    End Try
                End Using
            End Using
        End If
        Return returnValue
    End Function

这是我的高分类

Imports System.Collections.Generic
Imports System.Xml

Public Class HighScores
    Public scores As List(Of Score) = New List(Of Score)
    Public userplays As List(Of UserGameLog) = New List(Of UserGameLog)
    'Public userPlays As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
    Sub New()

    End Sub

    Private Sub UpdatePlays(ByVal Username As String)
        For Each userlog As UserGameLog In userplays
            If (userlog.username = Username) Then
                userlog.numberOfPlays += 1
                Return
            End If
        Next
        userplays.Add(New UserGameLog(Username))
    End Sub

    Public Function GetNumberOfPlays(ByVal Username As String) As Integer
        For Each userlog As UserGameLog In userplays
            If (userlog.username = Username) Then
                Return userlog.numberOfPlays
            End If
        Next
        Return 0
    End Function

    Public Function GetHighScores() As List(Of Score)
        Return scores
    End Function

    Public Sub AddNewScore(ByVal score As Score)
        UpdatePlays(score.username)
        If (scores.Count > 0) Then
            For index = 0 To scores.Count - 1
                If (scores(index).score < score.score) Then
                    scores.Insert(index, score)
                    Exit Sub
                End If
            Next

        End If
        scores.Add(score)

    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

解决了我的问题,在加载高分榜时我拿出了5个最高分。而不是使用范围我分配了一个新的列表分数列表并删除未使用的值,以使用

将其修剪为5
Public Function GetHighScores(ByVal maxScoreCount As Integer) As List(Of Score)
        Dim scores As List(Of Score) = New List(Of Score)
        scores = _highScores.scores
        If (scores.Count > maxScoreCount) Then
            scores.RemoveRange(maxScoreCount - 1, scores.Count - maxScoreCount)
        End If
        Return scores
    End Function

这是对先前列表的引用,它正在削减我的价值观。用GetRange交换它并解决问题。 咂头