将新对象添加到通用列表会覆盖以前的值

时间:2010-07-15 14:23:41

标签: .net vb.net collections

我有一个通用列表的问题,它由我创建的类的intances组成。我很惊讶,每次我在列表中添加一个新对象时,它都会覆盖以前的条目。这是一个代码示例,我只是无法相信它。我正在使用visual studio 2010:

Dim translations As List(Of TranslatedValue) = New List(Of TranslatedValue)
If Session("ctlTexts") Is Nothing Then
    Dim reader As IDataReader = DataAccessFunctions.db.GetDataReader("SELECT [ControlID], [ControlTextEN], [ControlTextDE], [ControlTextBG] FROM [dbo].[tbLanguage]", GetConnectionString)

    While reader.Read()
        Dim translationBulgarian As String = String.Empty
        Dim translationGerman As String = String.Empty
        Dim translationEnglish As String = String.Empty
        Dim translationKey As String = String.Empty
        Dim currentLanguageTranslation As String = String.Empty

        If Not reader.IsDBNull(0) Then
            translationKey = reader("ControlID")
        End If
        If Not reader.IsDBNull(1) Then
            translationEnglish = reader("ControlTextEN")
        End If
        If Not reader.IsDBNull(2) Then
            translationGerman = reader("ControlTextDE")
        End If
        If Not reader.IsDBNull(3) Then
            translationBulgarian = reader("ControlTextBG")
        End If

        Select Case CurrentLanguage
            Case Language.Bulgarian
                currentLanguageTranslation = translationBulgarian
            Exit Select
            Case Language.English
                currentLanguageTranslation = translationEnglish
            Exit Select
            Case Language.German
                currentLanguageTranslation = translationGerman
            Exit Select
        End Select

        translations.Add(New TranslatedValue(translationKey, translationEnglish, translationGerman, translationBulgarian, currentLanguageTranslation))
    End While
reader.Close()
Session("ctlTexts") = translations

2 个答案:

答案 0 :(得分:1)

从根本上说,你必须问自己:List(of T)如何知道如何比较两个TranslatedValues。

答案 1 :(得分:0)

我太蠢了! :)我已经在我的班级中声明了静态私人成员:)抱歉打扰你,谢谢你的回复!