views:

40

answers:

4

Hi guys, 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
+1  A: 

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

GregC
To me, it sounds like you're better off using a Dictionary(Of T)
GregC
How does this matter?
dkson
@dkson - A Dictionary(Of T) will require each entry to be unique by some key. This will at least tell you if you have collisions.
Joel Etherton
@Joel - I just don't see how the code could not work, since he is clearly adding new objects to the list.
dkson
@dkson - I agree that it appears that is what should be happening, but clearly it is not if that is not the result he sees. Going based on that information, I would have to agree that trying a Dictionary would help eliminate the possibility of a compare issue.
Joel Etherton
@Joel I think this kind of problems are quite easy to solve if you know how to use a debugger
dkson
@dkson - My assumption is that he does know how to use the debugger and in this instance is unable to glean meaningful information from it because no exception is being produced.
Joel Etherton
A: 

What do you mean? I each time add a new instance of the class to the list. I just don't get it, because i have many running examples, where i have a list of objects. I also created a sample project and everything worked. I have no idea.

neonglow
A: 

well that really amazes me! Each time i add an object to the list, it overwrites all previous values! Here is a screenshot: http://www.myhomebills.info/genericlistoverwrited.gif

I don't get it, i add a new instance of the class each time. I tried deleting the intance but didn't work either. When reproducing this scenario in a different project it works fine:) The class i have this loop inherits System.Web.UI.Page, could this have anything to do with it?

neonglow
A: 

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

neonglow