Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is the following static properties on the <code>ReadFromFile</code> class, if I assume by "overwritten" you mean that both instances of <code>UserFile</code> point to the same data:</p> <pre><code> public static Dictionary&lt;int, Family&gt; families = new Dictionary&lt;int, Family&gt;(); public static Dictionary&lt;int, Individual&gt; individuals = new Dictionary&lt;int, Individual&gt;(); public static Header head; </code></pre> <p>The problem lies in the constructor of <code>UserFile</code> on the use of <strong>static</strong> properties.</p> <pre><code> ReadFromFile.Read(_dir); fileIndividuals = ReadFromFile.individuals; // &lt;-- Uh-oh! fileFamilies = ReadFromFile.families; // &lt;-- Uh-oh! fileHead = ReadFromFile.head; // &lt;-- Uh-oh! </code></pre> <p>What happens here is the member variables <code>fileIndividuals</code>, <code>fileFamilies</code> and <code>fileHead</code> are set to a <strong>reference</strong> of the <code>individuals</code>, <code>families</code> and <code>head</code> properties on the static <code>ReadFromFile</code> class, <strong>not a copy</strong> (as they are classes and not value types). So the next time <code>ReadFromFile.Read()</code> is called the static properties on <code>ReadFromFile</code> are updated (overwritten), but the prior instance of <code>UserFile</code> just points to the <em>same</em> static properties, ergo <code>file1</code> and <code>file2</code> will have the same data.</p> <p>So how would you fix this? Two options:</p> <ol> <li>Make <code>ReadFromFile</code> and instance class, and not a static one. Construct a new instance in the <code>UserFile</code> constructor and don't use <strong>any</strong> static properties.</li> <li>Make a copy of the data in <code>individuals</code>, <code>families</code> and <code>head</code> in the constructor of <code>UserFile</code>. "foreach" through each item, and copy it into a new dictionary. </li> </ol> <hr> <p>Simple explanation: </p> <p>When you do an assign (the = character in C#) if the object is a class, then the target is assigned a "pointer" (reference) to the right hand side. If it's a value type it's copied. Dictionary is a class, so you get a pointer and not a copy. </p> <p>Illustration in code: </p> <pre><code>public static class MyStaticClass { public static List&lt;string&gt; MyList = new List&lt;string&gt; } </code></pre> <p>Elsewhere...</p> <pre><code>public void MyMethod() { List&lt;string&gt; myList1 = MyStaticClass.MyList; List&lt;string&gt; myList2 = MyStaticClass.MyList; myList1.Add("Hello"); // Add to first list myList2.Add("World"); // Add to second list foreach(string item in myList1) // print all items in the second list { Console.WriteLine("List 1: " + item); } foreach(string item in myList2) // print all items in the second list { Console.WriteLine("List 2: " + item); } } </code></pre> <p>The output of this will be: </p> <pre><code>List 1: Hello List 1: World List 2: Hello List 2: World </code></pre> <p>But why? We only added 'World' to <code>myList2</code>. Well <code>myList1</code> and <code>myList2</code> <strong>point</strong> to the same thing. When we did <code>myList1 = MyStaticClass.MyList</code> we didn't get a <strong>copy</strong> of the item, just a <strong>reference</strong> to it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload