473,320 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Adding items to a List(of object

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

Here's my original code
Expand|Select|Wrap|Line Numbers
  1. Dim BeaconStoryList as New List(Of My.StoryType)
  2. Dim BeaconStory as New My.StoryType
  3. Dim NewStories() as New Foriegn.StoryType = GetStories
  4.  
  5. For Each Story as Foriegn.StoryType in NewStories
  6.      If Story.Type = My.BeaconType then
  7.          BeaconStory.Title = Story.storyTitle
  8.          BeaconStory.Body = Story.storyBody
  9.          BeaconStory.Type = Story.storyType
  10.          BeaconStoryList.Add(BeaconStory)
  11.     End if
  12. Next
  13.  
This seamed pretty straight forward to me. There might be 20 stories, 5 of which were of type "My.BeaconType" 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
Expand|Select|Wrap|Line Numbers
  1. Dim BeaconStoryList as New List(Of My.StoryType)
  2. Dim NewStories() as New Foriegn.StoryType = GetStories
  3. Dim I as Integer = 0
  4.  
  5. For Each Story as Foriegn.StoryType in NewStories
  6.      If Story.Type = My.BeaconType then
  7.          BeaconStoryList.Add(New My.StoryType)
  8.          BeaconStoryList(I).Title = Story.storyTitle
  9.          BeaconStoryList(I).Body = Story.storyBody
  10.          BeaconStoryList(I).Type = Story.storyType
  11.          I +=1
  12.     End if
  13. Next
  14.  
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.Title 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.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
Mar 28 '10 #1
3 11806
tlhintoq
3,525 Expert 2GB
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.
Mar 28 '10 #2
!NoItAll
297 100+
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. For Each Story as Foriegn.StoryType in NewStories
  3.       If Story.Type = My.BeaconType then
  4.           BeaconStory.Title = Story.storyTitle
  5.           BeaconStory.Body = Story.storyBody
  6.           BeaconStory.Type = Story.storyType
  7.           BeaconStoryList.Add(BeaconStory)
  8.      End if
  9.  Next
  10.  
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. For Each Story as Foriegn.StoryType in NewStories
  3.       If Story.Type = My.BeaconType then
  4.           BeaconStory = New My.StoryType
  5.           BeaconStory.Title = Story.storyTitle
  6.           BeaconStory.Body = Story.storyBody
  7.           BeaconStory.Type = Story.storyType
  8.           BeaconStoryList.Add(BeaconStory)
  9.      End if
  10.  Next
  11.  
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...
Mar 28 '10 #3
!NoItAll
297 100+
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

Expand|Select|Wrap|Line Numbers
  1. Public Class Story_type
  2.     Private vBody As String
  3.     Private vTitle As String
  4.     Private vType As Integer = 0
  5.  
  6.     Public Property Body() As String
  7.         Get
  8.             Return vBody
  9.         End Get
  10.         Set(ByVal value As String)
  11.             vBody = value
  12.         End Set
  13.     End Property
  14.  
  15.     Public Property Title() As String
  16.         Get
  17.             Return vTitle
  18.         End Get
  19.         Set(ByVal value As String)
  20.             vTitle = value
  21.         End Set
  22.     End Property
  23.  
  24.     Public Property Type() As Integer
  25.         Get
  26.             Return vType
  27.         End Get
  28.         Set(ByVal value As Integer)
  29.             vType = value
  30.         End Set
  31.     End Property
  32.  
  33. End Class
  34.  
And then to set up the List(of I do this:

Expand|Select|Wrap|Line Numbers
  1. Public Class ArrayOfStory_type
  2.     Inherits List(Of Story_type)
  3. End Class
  4.  
Is this cool - or weird?
Mar 28 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tina Li | last post by:
Hello, I have a cascade menu all of whose items I want to delete. It's natural to pick up deletemenuitems(menuname, start, end). But what would the 'end' be? Is there a list of menu items...
1
by: akash D | last post by:
I have two list boxes, I want to filter items of second list box based on selcted items of First list box *** Sent via Developersdex http://www.developersdex.com ***
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
2
by: maflatoun | last post by:
Hi, I'm wondering if there is a way to bind to datagrid or a gridview the items in an arraylist. These items all have vars such as price, qty..etc? (besides putting in a datagrid or datatable...
1
by: Deckarep | last post by:
Dear CSharp Group, Both of these techniques work as expected but what is the better way of doing this? Or does it even make a difference? Method 1: public delegate void MyDelegate(string...
3
by: UJ | last post by:
Is there a way to have items in a drop down list where certain items are a different color? How about having items that are strikethrough? TIA - Jeff.
10
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list...
5
by: Bob B. | last post by:
I've been playing with descriptors lately. I'm having one problem I can't seem to find the answer to. I want to assign a descriptor to a list of attributes. I know I should be able to add these...
3
by: kimiraikkonen | last post by:
Hi, I want to display ALL the items (includes unselected items) of listbox control. How can i code this? Should i use "for each" or other? For example: Assume you have a listbox with 3 items...
3
by: Gandalf | last post by:
my manual contain chapter about lists with python. when i try to copy paste : li = (1) it i get this errore: "TypeError: 'list' object is not callable"
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.