title: C# Filling a 2D array with objects from a txt file at specific positions
body: <p>Im trying to fill a 2D array with a txt file from my computer.</p>
  <p>I have tried doing this in two different ways but i have not gotten either to work.</p>
  <ol>
  <li><p>Save objects position number in the file using the following code.</p>
  <pre><code>             for (int row = 0; row &lt; 100; row++)
           {
               for (int col = 0; col &lt; 2; col++)
               {
                   if (cpp[row, col] == null)
                   {
                       break;
                   }
                   else
                   {
                       if (cpp[row, col] == null) continue;
                       save.WriteLine(string.Format(&quot;{0}, {1}, {2}, {3}&quot;, row + 1, cpp[row, col].RegNumber, cpp[row, col].VehicleTypes, cpp[row, col].TimeOfArrival));
                   }
               }
           }
  </code></pre>
  </li>
  </ol>
  <p>Second way i have tried is to save the file with the empty lines aswell.</p>
  <pre><code> foreach (var temp in cpp)
              {
                  if (temp == null) save.WriteLine(&quot;&quot;);
                  if (temp == null) continue;
                 save.WriteLine(&quot;{{0},{1},{2}&quot;, temp.RegNumber, temp.VehicleTypes, temp.TimeOfArrival);
              }
  </code></pre>
  <p>I have gotten nowhere with the load function and i would appreciate any help i can get with this. I need to load the file in to my 2D array and the objects have to be assigned to the same position it was before.</p>
  <p>The only code i have for the load function is this.</p>
  <pre><code>string[] lines = File.ReadAllLines(&quot;CarParkPrague.txt&quot;);
              var vehicles = new List&lt;Vehicle&gt;();
              {
                  foreach (var line in lines)
                  {
                      var values = line.Split(&quot;,&quot;);
                      Enum.TryParse(values[1], out VehicleType type);
                      var time = DateTime.Parse(values[2]);
                      vehicles.Add(new Vehicle(values[0], type, time)); 
                      
                  }
              }
  </code></pre>
  <p>Text file line  example = AAA111,CAR,2020-10-11 14:19:04
  Or with the position saved = 1,AAA111,CAR,2020-10-11 14:19:04
  Can be either, which ever is easiest.</p>
  <p>This is a test method that works but it saves the vehicles on first best available spot in the array so its not quite right. The text file is in this case saved without a position number. And looks like the first example above.</p>
  <pre><code>static void LoadTestFile() //Loads the array using the test file which is filled with examples.
      {
          string[] lines = File.ReadAllLines(&quot;testfile.txt&quot;);
          var vehicles = new List&lt;Vehicle&gt;();
  
          foreach (var line in lines)
          {
              var values = line.Split(&quot;,&quot;);
              Enum.TryParse(values[1], out VehicleType type);
              var time = DateTime.Parse(values[2]);
              vehicles.Add(new Vehicle(values[0], type, time));
          }
  
          int counter = 0;
          for (int i = 0; i &lt; 100; i++)
          {
              for (int j = 0; j &lt; 2; j++)
              {
                  if (counter &gt;= vehicles.Count)
                      break;
  
                  if (cpp[i, 0] != null)
                  {
                      if (cpp[i, 0].VehicleTypes == VehicleType.CAR)
                      {
                          continue;
                      }
                  }
  
                  if (vehicles[counter].VehicleTypes == VehicleType.CAR)
                  {
                      cpp[i, j] = vehicles[counter++];
                  }
                  else
                  {
                      cpp[i, j] = vehicles[counter++];
                  }
              }
          }
  </code></pre>