将新对象添加到generit列表会覆盖以前的值
我对通用列表有疑问,该列表由我创建的类的实例组成。我很惊讶,每次我向列表中添加一个新对象时,它都会覆盖以前的条目。这是一个代码示例,我简直不敢相信。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从根本上讲,您必须问自己:List(of T) 如何知道如何比较两个 TranslatedValues。
Fundamentally, you gotta ask yourself: how does the List(of T) know how to compare two TranslatedValues.
我真是太傻了! :) 我已经在我的班级中声明了静态私有成员:) 抱歉打扰您,感谢您的回复!
I'm so stupid! :) I have declared static private members in my class :) Sorry for bothering you, and thanks for your replies!