Hi,
I am getting an exception when I try to remove item in foreach loop.
Collection was modified; enumeration operation may not execute.
In this short article (
http://www.dotnetperls.com/invalidoperationexception[
^] ) they say that it cant be done this way but instead I could implement remove method in my own Plane class. -> but that doesn't work.
Is it possible to do it(to REMOVE item within foreach loop?)
Any comments or suggestions are welcome. Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace MyApplication
{
public partial class MainWindow : Window
{
DispatcherTimer arrivalClock;
List<Plane> planes = new List<Plane>();
List<Plane> planesLanded = new List<Plane>();
List<Plane> planesCrashed = new List<Plane>();
Random rnd;
public MainWindow()
{
rnd = new Random(System.Environment.TickCount);
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
arrivalClock = new DispatcherTimer();
arrivalClock.Interval = new TimeSpan(0, 0, rnd.Next(1, 5));
arrivalClock.Tick += new EventHandler(arrivalClock_Tick);
arrivalClock.Start();
}
void arrivalClock_Tick(object sender, EventArgs e)
{
if (planes.Count() != 0)
{
foreach (Plane p in planes)
{
if (p.Fuel <= 0)
{
planesCrashed.Add(p);
refreshCrashedList();
}
else if(p.Distance <= 0)
{
planesLanded.Add(p);
refreshLandedList();
}
planes.Remove(p);
refreshPlanesList();
}
foreach(Plane p in planes)
{
p.goForward();
}
}
planes.Add(new Plane());
refreshPlanesList();
}
private void refreshPlanesList()
{
lbxLanded.Items.Clear();
foreach (Plane p in planes)
{
lbxLanded.Items.Add(p);
}
}
private void refreshCrashedList()
{
lbxCrashed.Items.Clear();
foreach (Plane p in planesCrashed)
{
lbxCrashed.Items.Add(p);
}
}
private void refreshLandedList()
{
lbxLanded.Items.Clear();
foreach (Plane p in planesLanded)
{
lbxLanded.Items.Add(p);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyApplication
{
class Plane
{
string callSign;
byte noOfPassengers;
int fuelOnBoard;
int fuelSpent;
decimal speed;
decimal distance;
DateTime arrivedTime;
Random rnd = new Random();
public Plane()
{
callSign = getName();
noOfPassengers = getPassengers();
fuelOnBoard = getFuelOnBoard();
fuelSpent = getFuelSpent();
speed = getSpeed();
distance = getDistance();
arrivedTime = DateTime.Now;
}
#region
public int Fuel
{
get { return fuelOnBoard; }
}
public decimal Distance
{
get { return distance; }
}
#endregion
public override string ToString()
{
return String.Format("{0} : passengers = {1}, fuel = {2}, distance = {3}", callSign, noOfPassengers, fuelOnBoard, distance);
}
private string getName()
{
char c = (char)rnd.Next(65, 91);
return c.ToString();
}
private byte getPassengers()
{
byte b = (byte)rnd.Next(0, 255);
return b;
}
private int getFuelOnBoard()
{
int i = (int)rnd.Next(2000, 10000);
return i;
}
private int getFuelSpent()
{
int i = (int)rnd.Next(200, 1000);
return i;
}
private decimal getSpeed()
{
decimal d = (decimal)rnd.Next(140, 400);
return d;
}
private decimal getDistance()
{
decimal d = (decimal)rnd.Next(1000, 10000);
return d;
}
public void goForward()
{
fuelOnBoard -= rnd.Next(500, 800);
}
}
}