Sortuję listę z moim własnym programem IComparer i działa to dobrze podczas uruchamiania aplikacji (gra XNA) przez ponad godzinę. Ale potem, nagle, czasami pojawia się następujący błąd podczas wywoływania sortowania-metody z moim niestandardowym porównywarka:Dlaczego pojawia się wyjątek System.ArgumentException podczas wywoływania sortowania (programu IComparer) na liście?
An unhandled exception of type 'System.ArgumentException' occured in mscorlib.dll
Additional Information: ArgumentException
Jest to linia, gdzie jest wyjątek:
List<Continent> markets = new List<Continent>();
// filling the markets list ...
markets.Sort(new MarketCostCoverComparer(this));
i to jest mój klasa implementująca interfejs IComparer:
class MarketCostCoverComparer : IComparer<Continent> {
private Player player;
public MarketCostCoverComparer(Player player) {
this.player=player;
}
public int Compare(Continent c1, Continent c2) {
if(player.GetCostCovering(c1)<player.GetCostCovering(c2)) {
return +1;
} else if(player.GetCostCovering(c1)==player.GetCostCovering(c2)) {
return 0;
} else {
return -1;
}
}
}
Oto kilka metod, które są związane z comparer ...:
public float GetCostCovering(Continent continent) {
// cover<1 => bad | cover>1 => good
if(GetOilfieldTheoreticOutput(continent.Type, true)<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 && 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 && oilfield.Drilled==drilled) {
total+=oilfield.Costs;
}
}
return total;
}
Oto zrzut ekranu z wyjątkiem:
Oto bliższe spojrzenie z mieszkańcami/Stack śledzenia (jest to stara zrzut ekranu, ale postaram się odtworzyć ten wyjątek w ciągu następnych godzin, tak że mogę rozwinąć śladu):
Czy możesz podać stacktrace wyjątku? –
1) opublikować wyjątek wewnętrzny, jeśli istnieje jeden, lub przynajmniej stos 2 ślad), należy opublikować metodę GetCostCovering() – Filip
Niestety nie ma śledzenia stosu, ponieważ Visual Studio po prostu wyskakuje okienko z dwoma wyżej wymienionymi wierszami. Metody GetCostCovering() następują po ... – salocinx