Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I get a System.ArgumentException when invoking Sort(IComparer) on a List?
    primarykey
    data
    text
    <p>I sort a List with my own IComparer and this works just fine when running the application (XNA game) for more than an hour. But then, suddenly, I sometimes get the following error when invoking the sort-method with my custom Comparer:</p> <pre><code>An unhandled exception of type 'System.ArgumentException' occured in mscorlib.dll Additional Information: ArgumentException </code></pre> <p>This is the line where the exception is thrown:</p> <pre><code>List&lt;Continent&gt; markets = new List&lt;Continent&gt;(); // filling the markets list ... markets.Sort(new MarketCostCoverComparer(this)); </code></pre> <p>and this is my class implementing IComparer interface:</p> <pre><code>class MarketCostCoverComparer : IComparer&lt;Continent&gt; { private Player player; public MarketCostCoverComparer(Player player) { this.player=player; } public int Compare(Continent c1, Continent c2) { if(player.GetCostCovering(c1)&lt;player.GetCostCovering(c2)) { return +1; } else if(player.GetCostCovering(c1)==player.GetCostCovering(c2)) { return 0; } else { return -1; } } } </code></pre> <p>Here some methods that are linked to the comparer...:</p> <pre><code>public float GetCostCovering(Continent continent) { // cover&lt;1 =&gt; bad | cover&gt;1 =&gt; good if(GetOilfieldTheoreticOutput(continent.Type, true)&lt;continent.Economy.CurrentDemand) { return ((float)((GetOilfieldTheoreticOutput(continent.Type, true)*continent.Economy.CurrentPrice)))/(float)GetOilfieldCosts(continent.Type, true); } else { return ((float)((continent.Economy.CurrentDemand*continent.Economy.CurrentPrice)))/(float)GetOilfieldCosts(continent.Type, true); } } public int GetOilfieldTheoreticOutput(ContinentType continent, bool drilled) { int total = 0; foreach(Oilfield oilfield in worldmap.Continents[(int)continent].Oilfields) { if(oilfield.Owner==this &amp;&amp; oilfield.Drilled==drilled) { total+=oilfield.TheoreticOutput; } } return total; } public int GetOilfieldCosts(ContinentType continent, bool drilled) { int total = 0; foreach(Oilfield oilfield in worldmap.Continents[(int)continent].Oilfields) { if(oilfield.Owner==this &amp;&amp; oilfield.Drilled==drilled) { total+=oilfield.Costs; } } return total; } </code></pre> <p>Here the screenshot of the exception:</p> <p><img src="https://i.stack.imgur.com/LksNk.png" alt="An unhandled exception of type &#39;System.ArgumentException&#39; occured in mscorlib.dll"></p> <p>Here a closer look of the Locals/Stack-Trace (this is an old screenshot, but I will try to reproduce this exception within the next hours, so that I can expand the trace):</p> <p><img src="https://i.stack.imgur.com/I0jZj.png" alt="enter image description here"></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.
 

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