Here's my original code
Expand|Select|Wrap|Line Numbers
- 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
Expand|Select|Wrap|Line Numbers
- 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.Title, 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