List.Add seems to be duplicating entries. What's wrong?

前端 未结 6 1390
醉梦人生
醉梦人生 2020-12-09 23:35

I have a class like this:

public class myClass
{
  public List anewlist = new List;

  public void addToList(myOtherC         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 00:25

    Got it! Thank you James -

    here's the stupid thing I did wrong:

    I had:

    myClass tmpClass = new myClass();
    myOtherClass anewitem = new myOtherClass();
    string tst = "";
    
    for (int i = 0; i < 100; i++) 
    {
        tst += "blah";
        anewitem.theStirngInMyClass = tst;
        tmpClass.AddToList(anewitem);
    }
    

    when I changed it to be this:

    myClass tmpClass = new myClass();
    string tst = "";
    
    for (int i = 0; i < 100; i++) 
    {
        myOtherClass anewitem = new myOtherClass()
        tst += "blah";
        anewitem.theStringInMyClass = tst;
        tmpClass.AddToList(tst);
    }
    

    All was well. I get it. :)

    Thanks for the help guys!

    -Adeena

提交回复
热议问题