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

  • Home
  • SEARCH
  • 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 85507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:06:01+00:00 2026-05-10T22:06:01+00:00

I have a class like this: public class myClass { public List<myOtherClass> anewlist =

  • 0

I have a class like this:

public class myClass {   public List<myOtherClass> anewlist = new List<myOtherClass>;    public void addToList(myOtherClass tmp)   {     anewList.Add(tmp);   }  } 

So I call ‘addToList’ a hundred times, each adding a unique item to the list. I’ve tested my items to show that before I run the ‘addToList’ method, they are unique. I even put a line in to test ‘tmp’ to make sure it was what I was expecting.

However, when I do this (lets say myClass object is called tmpClass):

int i = tmpClass.anewList.Count(); for (int j = 0; j<i; j++) {    //write out each member of the list based on index j... } 

I get the same exact item, and it’s the last one that was written into my list. It’s as if when I add, I’m overwriting the entire list with the last item I’ve added.

Help? This makes no sense. I’ve also tried List.Insert, where I’m always inserting at the end or at index 0. Still no dice. Yes, I’m doubly source my indexing is correct and when I do my test I’m indexing through each of the elements.

🙂

UPDATE: Okay, I tried this and still had the same problem:

foreach(myOtherClass tmpC in tmpClass.anewList) {         Console.WriteLine(tmpC.theStringInMyClass.ToString()); } 

and still for each of the 100 items, I got the same string output… I’m sure I’m doing something completely stupid, but I don’t know what yet. I’m still 100% sure that the right string is getting passed in to begin with.

-Adeena


Okay, I tried this and still had the same problem:

foreach(myOtherClass tmpC in tmpClass.anewList) {     Console.WriteLine(tmpC.theStringInMyClass.ToString()); } 

and still for each of the 100 items, I got the same string output… I’m sure I’m doing something completely stupid, but I don’t know what yet. I’m still 100% sure that the right string is getting passed in to begin with.

-Adeena

  • 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. 2026-05-10T22:06:02+00:00Added an answer on May 10, 2026 at 10:06 pm

    Given the signature of your addToList method:

    public void addToList(myOtherClass tmp)   {     anewList.Add(tmp);   } 

    Is is possible that in the consumer of that method, you aren’t actually creating a new instance?

    You said that you are calling addToList 100 times. Presumably, that is in a loop. At each loop iteration, you will need to create a new instance of ‘myOtherClass’, otherwise, you’ll just be updating the same object in memory.

    For example, if you do the below, you will have 100 copies of the same object:

    myOtherClass item = new myOtherClass();  for(int i=0; i < 100; i++) {   item.Property = i;   addToList(item); } 

    However, if your loop looks like the below, it will work fine:

    myOtherClass item = null; for(int i=0; i < 100; i++) {   item = new myOtherClass();   item.Property = i;   addToList(item); } 

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer This problem was caused by the Authentication Delegation value in… May 11, 2026 at 3:03 pm
  • added an answer Use EnumDeviceDrivers - there's an example here. May 11, 2026 at 3:03 pm
  • added an answer The FontSize property of the container should be inherited by… May 11, 2026 at 3:03 pm

Related Questions

I have a class that acts like this at the moment: public class MyClass
I have a class which looks something like this: class MyClass { public: //
I'm trying to find a way to fake the result of a method called
I'm writing a program under MS Visual C++ 6.0 (yes, I know it's ancient,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.