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

发布于 2024-09-10 05:07:48 字数 1822 浏览 2 评论 0原文

我对通用列表有疑问,该列表由我创建的类的实例组成。我很惊讶,每次我向列表中添加一个新对象时,它都会覆盖以前的条目。这是一个代码示例,我简直不敢相信。我正在使用 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

I have a problem with a generic list, which consists of intances of a class i created. I am just amazed, each time I add a new object to the list, it overwrites the previous entries. Here is a code sample, i just can't beleive it. I am using 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖风昔人 2024-09-17 05:07:48

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

Fundamentally, you gotta ask yourself: how does the List(of T) know how to compare two TranslatedValues.

烟火散人牵绊 2024-09-17 05:07:48

我真是太傻了! :) 我已经在我的班级中声明了静态私有成员:) 抱歉打扰您,感谢您的回复!

I'm so stupid! :) I have declared static private members in my class :) Sorry for bothering you, and thanks for your replies!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文