Adding items to a List(of object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #1

    Adding items to a List(of object

    I have some code that adds items to a List(of Object). It is behaving extremely bizarre!

    Here's my original code
    Code:
    Dim BeaconStoryList as New List(Of My.StoryType)
    Dim BeaconStory as New My.StoryType
    Dim NewStories() as New Foriegn.StoryType = GetStories
    
    For Each Story as Foriegn.StoryType in NewStories
         If Story.Type = My.BeaconType then
             BeaconStory.Title = Story.storyTitle
             BeaconStory.Body = Story.storyBody
             BeaconStory.Type = Story.storyType
             BeaconStoryList.Add(BeaconStory)
        End if
    Next
    This seamed pretty straight forward to me. There might be 20 stories, 5 of which were of type "My.BeaconT ype" and after the for each loop I would have a list(of collection of those 5 stories assigned to BeaconStoryList .
    But...
    After completing the loop every element in the collection would get the exact same values. Even though all 5 stories were different, all elements in the collection would get assigned the last values added to the end.
    To stop this bizarre behavior I had to change the code to this
    Code:
    Dim BeaconStoryList as New List(Of My.StoryType)
    Dim NewStories() as New Foriegn.StoryType = GetStories
    Dim I as Integer = 0
    
    For Each Story as Foriegn.StoryType in NewStories
         If Story.Type = My.BeaconType then
             BeaconStoryList.Add(New My.StoryType)
             BeaconStoryList(I).Title = Story.storyTitle
             BeaconStoryList(I).Body = Story.storyBody
             BeaconStoryList(I).Type = Story.storyType
             I +=1
        End if
    Next
    There must be something I do not understand that is going on. In the first code example I could step through the code with watch-points and actually see all of the indexes (0 through the current one) change value. For example: If I was on the first loop the BeaconStory.Tit le might be "Gravy". Then passing through the second loop the next story title would be "Chicken" so I would assume that BeaconStoryList (0).Title would be "Gravy" and BeaconStoryList (1).Title would be "Chicken".
    But as soon as the code assigned "Chicken" to BeaconStory.Tit le, then BeaconStoryList (0).Title would also change to "Chicken".
    With my second version of the code this did not happen.
    It's as though it was placing pointers into the elements of the array rather than values. If that is the case, however, then why does my second version of the code work - I'm still passing in the same thing - aren't I?

    !NoItAll
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    In your Story class, are each of these properties set to static? If they are, then that is why you get the same values back.

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      Ok - figured it out. Again - my own lack of experience surfaces like a bloated body in a lake...
      ewwww!
      My problem is that I did not understand how to properly create an array of my class. It's not as simple as:

      Dim MyClassArray(20 ) as MyClass

      Nor is it as easy as

      Dim MyClassArray() as MyClass
      Redim MyClassArray(20 )

      At this point you do have 20 elements of the MyClass type, but there is nothing in them. Look at it in the debugger and you will see none of the individual elements from the MyClass appear. The next step is to fill in the elements of the class into each element of the array:

      For I = 0 to 20
      MyClassArray(I) = new MyClass
      Next I


      So that was PART of my problem. The other part of my problem is that the loop:

      Code:
       
      For Each Story as Foriegn.StoryType in NewStories
            If Story.Type = My.BeaconType then
                BeaconStory.Title = Story.storyTitle
                BeaconStory.Body = Story.storyBody
                BeaconStory.Type = Story.storyType
                BeaconStoryList.Add(BeaconStory)
           End if
       Next
      Was actually just putting a pointer from each BeaconStory into the BeaconStoryList . Whenever I assigned a new value to any of the elements in BeaconStory then ALL elements inside of the BeaconStoryList would match.

      To fix this all I needed to do was add one line of code within the loop

      BeaconStory = New My.Storytype

      So the code looks like this now:
      Code:
       
      For Each Story as Foriegn.StoryType in NewStories
            If Story.Type = My.BeaconType then
                BeaconStory = New My.StoryType
                BeaconStory.Title = Story.storyTitle
                BeaconStory.Body = Story.storyBody
                BeaconStory.Type = Story.storyType
                BeaconStoryList.Add(BeaconStory)
           End if
       Next
      The addition of BeaconStory = New My.StoryType created a new copy of Beaconstory so that unique elements were created for addition into the BeaconStoryList .

      This seemed confusing to me at first - but it appears that with created classes all you are ever passing in and out are pointers to the values (byRef) and not the actual data.

      Still learning...

      Comment

      • !NoItAll
        Contributor
        • May 2006
        • 297

        #4
        Interesting tlhintoq - I don't specifically create anything as Static - I don't even know how to do that... Is it a default?

        Here's an example of one of my classes

        Code:
        Public Class Story_type
            Private vBody As String
            Private vTitle As String
            Private vType As Integer = 0
        
            Public Property Body() As String
                Get
                    Return vBody
                End Get
                Set(ByVal value As String)
                    vBody = value
                End Set
            End Property
        
            Public Property Title() As String
                Get
                    Return vTitle
                End Get
                Set(ByVal value As String)
                    vTitle = value
                End Set
            End Property
        
            Public Property Type() As Integer
                Get
                    Return vType
                End Get
                Set(ByVal value As Integer)
                    vType = value
                End Set
            End Property
        
        End Class
        And then to set up the List(of I do this:

        Code:
        Public Class ArrayOfStory_type
            Inherits List(Of Story_type)
        End Class
        Is this cool - or weird?

        Comment

        Working...