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 9115645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:28:52+00:00 2026-06-17T04:28:52+00:00

For some reason I cannot seem to identify what I am doing wrong. I

  • 0

For some reason I cannot seem to identify what I am doing wrong. I have tried a few things and each time the original record is modified.

I have a list of a particular class. I am iterating through the list, if a specific condition is met, I need to create a duplicate item for this iteration, modify 2 values, then add it to the final list. Except everytime I modify the duplicate I created, the values are modified in the original. The data is a binding list that is set to a list.

// MyClasses is a BindingList<MyClass>
List<MyClass> classesToSave = new List<MyClass>();
foreach (var class in MyClasses)
{
    classesToSave(class);

    if (class.NeedsDupe)
    {
        MyClass dupeClass = new MyClass();
        dupeClass = class;
        dupeClass.NeedsDupe = false;
        dupeClass.IsDupe = true;
        classesToSave(dupeClass);
    }
}

Lets say that through the first iteration, the first item in MyClasses has class.NeedsDupe == true and class.IsDupe == false. When class is added to classesToSave before the condition is hit, the only entry in classesToSave is the original class (class.NeedsDupe == true and class.IsDupe == false). However, when the condition is hit and the dupeClass object is modified and before dupeClass is added to classesToSave, the original entry (and only entry) in classesToSave is then modified.

Am I missing something completely? I have a feeling that I am drawing a brain fart and maybe need a reference to the old object? Or am I somehow copying the properties of the BindingList and when I change the dupeClass object that I think is a new object I am actually causing the change to ripple back through?

Thanks for any help.

  • 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-17T04:28:54+00:00Added an answer on June 17, 2026 at 4:28 am

    That’s the nature of classes, they are reference type. Create a copy constructor for MyClass

    class MyClass
    {
       public MyClass(MyClass myClass)
       {
           //Assign the properties here
           //this.x = myClass.x;
           //....
           //....
       }
    }
    

    then you call

    MyClass dupeClass = new MyClass(@class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason I cannot seem to get a ko.computed observable to compute when
I have a search setup inside a div but for some reason cannot get
To keep it simple, I've installed the SDK and for some reason cannot seem
For some reason I cannot seem to get IE8 to display my jqgrid right
For some reason, I cannot seem to get the value from my inputs. I
I cannot seem to get css transitions to work for some reason. I am
For some reason, I am receiving the following MySQL Error, but I cannot seem
I've been looking around on Google for quite some time and cannot seem to
i have a simple C program that uses CRITICAL_SECTION. for some reason it seem
For some reason, I can not seem to change the default position of an

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.