?
Solved

ArrayList of Objects OverWriting One Property Of The Object

Posted on 2006-10-25
11
Medium Priority
?
310 Views
Last Modified: 2010-04-16
So basically, I have an arraylist of type section. section is a class with a couple of properties, a count of the panels in the section, a name of the section, a weight of the section, an area of the section, and an arraylist of type Piece.

Piece is a class with properties for qty, length, name, description, cost, and the way its measured (per piece or per linear foot).

So each section is made up of a bunch of pieces.

The basic flow of the code is something along the lines of:
-get the list of the sections entered (there can be 1-12 sections, based on user input).
-initialize the sections with a list of Pieces that has length/qty/etc set to zero.
-add the sections to the sections' arraylist
-read an inport file associated with each section, parse through it for panel count, weight, area, etc, and update the section's arraylist.

Below is the code for that last step (reading the file is it's own function).

What is happening, is after the 4th line (readFile()), my tempSectionB was updated correctly. It has the right weight, area, and piece list. Loop through to the next section, and again, it has the correct section object for the import file. However, after adding it to the section's list, it overwrites the first section in the list's piece list. Everything else in the first and second section is different/valid (they have their own weight, area, panels, etc), but both now have the exact same piece list. Despite on the previous loop it having a different list.

So, first loop we have a piece list with a total qty of the pieces is, lets say 100. Second loop we have a piece list with a total qty of the pieces is 200. Then, magically, our FinalSectionList both have the qty of 200.

foreach(section tempSection in SectionsList)
{
      HtmlInputFile uploadedFile = tempSection.getFileForSection();
      string fileLoc = Server.MapPath("tempfiles//" + uploadedFile.Value.Substring(uploadedFile.Value.LastIndexOf("\\")+1));
      uploadFile(fileLoc, uploadedFile);
      section tempSectionB = readFile(fileLoc, tempSection);
      if(tempSectionB!=null)
            FinalSectionList.Add(tempSectionB);
      else
            return;
}

Any ideas why the piece lists are overwriting each other? We can't figure it out.
0
Comment
Question by:GENTP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 17804099
Foreach loops have a problem in that they are "read-only".  The don't throw an error, but they don't behave correctly.  Change your loop to loop using an index and the problem will probably go away.

You can read more at http://www.thescripts.com/forum/thread251568.html
0
 
LVL 5

Author Comment

by:GENTP
ID: 17805372
I swapped the code above for this, and still no luck:

listOfSections = new section[SectionsList.Count];
listOfSections = (section[])SectionsList.ToArray(typeof(section));
EmptyPieceList = null;
for(i=0;i<SectionsList.Count-1;i++)
{
      section tempSection = listOfSections[i];
      HtmlInputFile uploadedFile = tempSection.getFileForSection();
      string fileLoc = Server.MapPath("tempfiles//" + uploadedFile.Value.Substring(uploadedFile.Value.LastIndexOf("\\")+1));
      uploadFile(fileLoc, uploadedFile);
      section tempSectionB = readFile(fileLoc, tempSection);
      if(tempSectionB!=null)
            FinalSectionList.Add(tempSectionB);
      else
            return;
}
0
 
LVL 21

Expert Comment

by:mastoo
ID: 17805533
Not enough code to really tell, but:

1) Where do you initialize FinalSectionList?
2) readFile isn't modifying tempSection?
3) You know listOfSections is a shallow copy of SectionsList meaning they both point to the same underlying section instances (assuming section is a class)?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:GENTP
ID: 17805682
1) FinalSectionList is declared earlier, but as an empty arraylist (ArrayList FinalSectionList = new ArrayList();). I have a method of the section class to update it's piece list, by passing in an arraylist of pieces, but in testing I switched it to adding the modified sections to a clean arraylist, hoping it would solve this problem (which it obviously didn't).

2) Readfile get's passed the the instance of tempSection. It then modifies it as needed, and returns the local copy of the section back to the calling function (thus assigning it to a new section).

3) Yes, section is a class.

Hope this gives you something to go on, and hopefully some suggestions.

Thanks
0
 
LVL 21

Expert Comment

by:mastoo
ID: 17805880
Speaking generally, you've either got two section variables referring to the same section instance, or two section instances referring to the same pieces arraylist.  Break after adding the 200 pieces to section 2.  Look at the name of section 1 and 2 to be sure they are different.  Then look at GetHashCode for the pieces arraylist in each section.  If they return the same hash code you've got two sections pointing at the same arraylist.
0
 
LVL 5

Author Comment

by:GENTP
ID: 17806199
All sections are pointing to the same pieces arraylist, based on hashcodes.

So that narrows down the problem a bit. Any idea why they are doing this though? I don't see a spot where I am confusing them in my code.
0
 
LVL 21

Accepted Solution

by:
mastoo earned 1500 total points
ID: 17806468
I'd have to see your code where you new the arraylist and assign it.  I slapped together a quick sample to make sure GetHashCode does what I thought:

      System.Collections.ArrayList a = new System.Collections.ArrayList();
      System.Collections.ArrayList b = a;
      System.Collections.ArrayList c = new System.Collections.ArrayList();
      Console.WriteLine( "a is " + a.GetHashCode().ToString() );
      Console.WriteLine( "b is " + b.GetHashCode().ToString() );
      Console.WriteLine( "c is " + c.GetHashCode().ToString() );

gives an output of:

a is 4
b is 4
c is 7

because b just points at a, whereas c is a separate instance.
0
 
LVL 5

Author Comment

by:GENTP
ID: 17852379
I've made some changes, and now the hash codes are different at all times throughout the function, however, the end result is the same contents of the arraylists.

Here is the print outs of hashcodes for 3 sections at 3 diferent points throughout the function (the final one is at the very end).

90
91
92
93
B 94
B 95
B 96
B 97
fin 217
fin 366
fin 515
fin 664
0
 
LVL 5

Author Comment

by:GENTP
ID: 17917031
Turns out that I was overwriting it at another part of code (passing the default list by ref, and continually overwriting it with the new updated list).

It was using the getHash method that I finally tracked it down however.

Thanks mastoo, your help in the second thread was appreciated as well.
0
 
LVL 21

Expert Comment

by:mastoo
ID: 17917110
Glad to help
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question