List<Object> adding Objects incorrectly
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: List<Object> adding Objects incorrectly

  1. #1
    Join Date
    May 2010
    Location
    .Net 4.0
    Posts
    58

    List<Object> adding Objects incorrectly

    Hello,

    I've run into a problem with my code. The following is a small snippet which focuses on the issue. Note that in this snippet, I am within nested for loops iterating through a List<Objects.File> class containing a List<Objects.Model> class, which itself contains a List<Objects.Entry> class. HTReplacers is simply an array of strings previously obtained to identify where editing the file is required.

    Code:
    foreach (string[] HTR in HTReplacers)
                                {
                                    if (OrigFiles[i].Models[j].Entries[k].ChainChar == Convert.ToChar(HTR[0]) && OrigFiles[i].Models[j].Entries[k].ResidueNum == Convert.ToInt32(HTR[1]) && (OrigFiles[i].Models[j].Entries[k].AtomType == "NH" || OrigFiles[i].Models[j].Entries[k].AtomType == "H"))
                                    {
                                        Objects.Entry Temp = OrigFiles[i].Models[j].Entries[k]; Temp.AtomType = "HT1"; Temp.ObjectNumber = Temp.ObjectNumber - ObjectNumRemoved + ObjectNumAdded; NewModel.Entries.Add(Temp); SkipAdd = true;
                                        ObjectNumAdded++;
                                        Objects.Entry Temp2 = OrigFiles[i].Models[j].Entries[k]; Temp2.AtomType = "HT2"; Temp2.ObjectNumber = Temp2.ObjectNumber - ObjectNumRemoved + ObjectNumAdded; NewModel.Entries.Add(Temp2);
                                        ObjectNumAdded++;
                                        Objects.Entry Temp3 = OrigFiles[i].Models[j].Entries[k]; Temp3.AtomType = "HT3"; Temp3.ObjectNumber = Temp3.ObjectNumber - ObjectNumRemoved + ObjectNumAdded; NewModel.Entries.Add(Temp3);
                                    }
                                }
    The problem is, when I write my edited data back to a file (simply a sequential iteration through all files/models/entries in which I print the data in the objects in the format shown below), I get the following piece:

    Code:
    ATOM      8  N   HIS     2       4.011  -1.442  -1.143  1.00  50.12     A
    ATOM     12  HT3 HIS     2       4.449  -0.680  -0.710  1.00  34.15     A
    ATOM     12  HT3 HIS     2       4.449  -0.680  -0.710  1.00  34.15     A
    ATOM     12  HT3 HIS     2       4.449  -0.680  -0.710  1.00  34.15     A
    ATOM     12  CA  HIS     2       4.767  -2.671  -1.355  1.00  31.34     A
    Note that aside from the numbering issue, it is making 3 HT3 atoms instead of an HT1, HT2, and HT3 as specified in my code. I'm not sure why; I made three separate objects and added them sequentially to the list. If someone sees what I'm doing wrong, could you please point me to it?

  2. #2
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: List<Object> adding Objects incorrectly

    Hello

    You use
    Objects.Entry Temp = OrigFiles[i].Models[j].Entries[k];
    Objects.Entry Temp2 = OrigFiles[i].Models[j].Entries[k];
    Objects.Entry Temp3 = OrigFiles[i].Models[j].Entries[k];

    SO Temp, Temp2 and Temp3 all reference the same object.
    When you set properties on Temp3 you are setting properties on Temp2 and Temp.
    You then add three copies of this same object into your NewModel.Entries collection.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center