Given following code:
tasks = new StationTasks[stations.Length * configurations.Length];
int taskNr = -1;
for (int i = 0; i < stations.Length; ++i) {
foreach (Configuration conf in configurations) {
++taskNr; // line A
StationTasks st = new StationTasks( conf, stations[i]); // line B
tasks[taskNr] = st; // line C
ArrayList tasksRaw = getTasks(conf, stations[i].Col_ID.GetInt());
// determine max length
int lengthStation = 0;
foreach (TaskItem ti in tasksRaw)
lengthStation += ti.TotalLength;
if (lengthStation > maxLength)
maxLength = lengthStation;
if (modeGroup == ModesGroup.Solid) {
// ...
} else if (modeGroup == ModesGroup.Category) {
ArrayList categories = new ArrayList();
foreach (TaskItem ti in tasksRaw) {
string key = ti.MarkerLColor.ToArgb().ToString() + ":" + ti.MarkerRText;
if (!categories.Contains(key))
categories.Add(key);
}
foreach (string key in categories) {
int key_i = key.IndexOf(":");
int colorID = int.Parse( key.Substring(0, key_i) );
Color color = Color.FromArgb( colorID );
if (color.ToArgb() == 0)
color = Color.Empty;
string cat = key.Substring(key_i + 1);
if (cat == "")
cat = null;
int length = 0;
foreach (TaskItem ti in tasksRaw)
if (ti.MarkerRText == cat && ti.MarkerLColor.ToArgb() == colorID)
length += ti.TotalLength;
if (length > 0) {
TaskItem ti = new TaskItem(null, null, length, Color.White);
ti.MarkerLColor = color;
ti.MarkerRText = cat;
st.TaskItems.Add(ti); // line D
}
}
}
}
}
where:
private class StationTasks {
private Configuration configuration;
public Configuration Configuration {
get { return configuration; }
set { configuration = value; }
}
private Station station;
public Station Station {
get { return station; }
set { station = value; }
}
private ArrayList taskItems;
public ArrayList TaskItems {
get { return taskItems; }
}
public StationTasks(Configuration conf, Station station) {
this.configuration = conf;
this.station = station;
taskItems = new ArrayList();
}
}
(the contents of Configuration, Station, TaskItem, etc. is not necessary
here)
how is it possible that the above code works well, whereas changing the
marked lines:
lines A, B, C to:
tasks[++taskNr] = new StationTasks( conf, stations[i]); // line B
and line D to:
tasks[taskNr].TaskItems.Add(ti); // line D
causes the program to behave buggy? For instance, when taskNr == 1, line D
would call tasks[0].TaskItems.Add(ti) instead of tasks[1]....