I have some code that adds items to a List(of Object). It is behaving extremely bizarre!
Here's my original code
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
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
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
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
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
Comment