In the pseudo code below, I would expect that trial_events would be a reference to the ship_list in pr.
The trial_events I get look like the ship_list, but it is out of order.
The actual code loads several ship lists, each belonging to a different “condition” and then in the game_controller I use the ship_list from the condition I want by assigning it to trial_events.
My issue is that trial_events does look like pr.ship_list, but the array is out of order. I can have trial_events and pr.ship_list visible in a watch, and pr.ship_list is as it should be, based on the file it read. trial_events has the same set of ships, but the last element of pr.ship_list is advanced in the trial_events array. That is, the 7th element of ship_list is in 5th element of trial_events with the 5th and 6th shifted to the 6th and 7th, as if it were inserted into the array at that position.
From the moment I assign trial_events = pr.ship_list, it is read only.
Moreover, I can put a breakpoint on the assignment trial_events = pr.shp_list and step through it. The rearrangement of pr.ship_list, as represented by trial_events, is present immediately after execution.
Any advice as to how I am screwing this up? Is there something I’m misunderstanding about the use of the reference types here? pr.ship_list isn’t destroyed until the end of the game.
public class pm
public class Shiptype
public Shiptype ship_list;
read a file and build a 2-d array of ships in ship_list;
public class game_controller
pm pr = new pm();
trial_events = pr.ship_list;
loaded = true;
Well, as @Devster2020 said it’s kinda hard to make much sense of the information you have provided. However many things you said in your description does not makes or at least are 100% wrong. If you “observe” that behaviour you described, we / you are missing important details here. Just to put out some facts:
- If you assign an array reference from one variable to the other, both are referencing the same array.
- Changes to the array elements done through one reference will also affect the element you see in the other since both reference the same array.
However if you for example use something like Array.Resize you will actually create a new array that is replacing the old one in that variable. So if you copy the reference into another variable and then use Array.Resize on one of them, you will have two seperate arrays.
Your code is all just plain C# and I can asure you that copying references from one field to another can not somehow duplicate the array or magically re-arrange elements. If you observe such behaviour, there has to be some code somewhere that actually does this.
Programming is 100% reliable. If it’s not, there are only two possible reasons:
- Your hardware is broken
- You don’t understand what your code actually does well enough.
The first point is kinda unlikely but always a possibility. Though most actual hardware defects usually have much greater, devastating effects so they usually can be ruled out quickly.
The second point has many different aspects to it. Either you use some code you don’t know well enough (could be some framework methods), but it could also mean you have written code that is actually behaves undeterministic due to race conditions or other external factors.
On the bottom line we know much less than you know about your code, so we can not really follow the code what it does if we don’t see the whole picture. Posting pseudo code just makes things worse.