Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:48:28+00:00 2026-06-08T10:48:28+00:00

I have an object called particle that has its own properties (position, velocity, etc.)

  • 0

I have an object called particle that has its own properties (position, velocity, etc.) and in my windows form I create a list of particles. This list of particles is then updated in the code (i.e. each particle’s position, velocity, etc. is updated in each iteration step).

What I want to do is add this List<Particle> to another list List<List<Particle>> each iteration (after clicking a button) so that I now have seperate lists of particles that I can compare.

This is what my code looks like (UpdateEngine is a class that creates a list of particles in its initialise method and then has other methods that update the values of the particles in its list):

public partial class frmMain : Form
{
    private List<List<Particle>> listPlist;

    private UpdateEngine Engine;

    ...

    public frmMain()
    {
        InitializeComponent();

        listPlist = new List<List<Particle>>();

        Engine = new UpdateEngine();
    }

    ...

    //pressing this button iterates through a specified number of iterations
    private void btPrep_Click(object sender, EventArgs e)
    {            
        //create the particles and add the first list to the list of lists
        Engine.Initialize();
        listPlist.Add(Engine.ParticleList);

        //iterate through the list of particles in Engine and update their properties
        for(i = 0; i <= iterations; i++)
        {
            Engine.Update();
            listPlist.Add(Engine.ParticleList);
        }
    }
}

What I see happening is that the first list is added before the iterations fine. The first list added in the for loop is added fine. Every list added thereafter changes all the lists in listPlist to be identicle to the current list.

An example of what I see when I run the code:

After initialization:

  • listPlist(0) > Particle(0) > Position = 0,0

After first iteration:

  • listPlist(0) > Particle(0) > Position = 0,0
  • listPlist(1) > Particle(0) > Position = 1,1

After next iteration:

  • listPlist(0) > Particle(0) > Position = 2,2
  • listPlist(1) > Particle(0) > Position = 2,2
  • listPlist(2) > Particle(0) > Position = 2,2

I’m not really sure how to fix this. Does anyone know why this is happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:48:30+00:00Added an answer on June 8, 2026 at 10:48 am

    From what I understand from your question, the problem is that you are trying to make copies of a reference item, which is why you see all the list items changing.

    You will need to make a deep clone of these list/particles so that they arent referencialy dependent.

    You might have to add a new Copy Method or Constructor to the particle object to achieve this.

    Something like

    public class Particle
    {
        public int SomeField;
    
        public Particle Copy()
        {
            return new Particle { SomeField = this.SomeField };
        }   
    
        public Particle(Particle copyFrom)
        {
            this.SomeField = copyFrom.SomeField;
        }
    }
    

    You can then create a copy of the original list something like

    List<Particle> copyList = new List<Particle>(originalList.Select(c => c.Copy));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object called Person that has the following properties: int id; Name
I have an object that has properties of another object and one called DataValue
I have an object called brain brain has 8 properties that contain an Animal
I have an object called Layer that has some attributes and some methodes. i
I have an object called Document that has a one-to-many relationship with DocumentPersonCc. I
I have a parent object called Page that has a List of objects called
I have an object called User, with properties like Username, Age, Password email etc.
i have an object called Person . It has properties First, Last, Age ,
I have an object called Result<T> that has a constructor which accepts an argument
If I have an object called Catalog that has a (nonatomic, retain) attrib called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.