c# - I have a loop that initializes a list of objects with values, but as soon as it exits the loop the objects become identical -


as title suggests initializing objects in list through loop. become identical when loop exits. can see during loop not same. when loop exits change last object entered.

public list<elevationlayout> layoutlist = new list<elevationlayout>();     public int layoutnumber { get; set; }     public int worldwidth { get; set; }      public random seed { get; set; }     public xysize dimleft { get; set; }   //i have narrowed down problem method     //==========================================================================================================================================================     //==========================================================================================================================================================     //==========================================================================================================================================================      public void init(world world) {         dimleft = new xysize();         elevationlayout layout = new elevationlayout();         dimleft.y = 0;         dimleft.x = world.size.x;         seed = new random((int)datetime.now.ticks);         worldwidth = (int)((world.size.x / 6.4) + (world.size.x / 64) - 1);         layoutnumber =  worldwidth + seed.next(-2, 3);         (int = 0; < layoutnumber; i++)         {             layout.type = seed.next(0, 2);             layout.width = (world.size.x / layoutnumber) + seed.next(0, ((dimleft.x / layoutnumber) / 2) + 1);              if (layout.width > dimleft.x)             {                 layout.width = dimleft.x;             }              dimleft.x -= layout.width;             layout.height = seed.next(world.size.y / 16, (world.size.y / 4) + 1);              if (layout.height > dimleft.y)             {                 layout.height = dimleft.y;             }               this.layoutlist.add(layout);             console.write(this.layoutlist[i].type); // here objects different              if ((world.size.y -= layout.height) > dimleft.y)             {                 dimleft.y = (world.size.y - layout.height);             }              if (dimleft.x <= 0)             {                 layoutnumber = i;             }         }         console.writeline();                 (int y = 0; y < layoutnumber; y++)                     console.write(this.layoutlist[y].type); //but exit loop same     }      //==============================================================================================================     //==============================================================================================================     //============================================================================================================== 

someone had similar problem here: why list of objects same? , here: why values in list same?

initially list of objects static, have since removed , problem remains.

the problem create 1 instance of object before loop , set properties of 1 object. fix:

    (int = 0; < layoutnumber; i++)     {           elevationlayout layout = new elevationlayout(); 

basically create object inside loop, on every iteration allocate new object.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -