[thelist] C#- using info from an arraylist

Robert Vreeland vreeland at studioframework.com
Sat Sep 9 09:16:33 CDT 2006


I was unable to retrieve the file, as the site was to busy - I'll try again
later. In the mean time, let me try to at least answer some of you
questions. 

>From the code in your post, your foreach loop is assigning a reference to
each item in the PassengersOnCar collection (array or ArrayList) as a
Passenger Object - which is not what I think you want to do. What I think
you were trying to do was get a reference to each ArrayList in the
PassengersOnCar collection. So you would need to write it like this

foreach(ArrayList intEndFloor in PassengersOnCar)
//foreach('type of object' 'variable name to use as reference' in 'the
collection to loop through')
So, in general, the 'type of object' should be the same type as stored in
the collection.

Knowing this, you would write your remove statement to use the variable
reference

 PassengersOnCar.Remove(intEndFloor)

This is an expensive proceedure, you are attempting to remove an item by
value rather then index position and is equivalent to relooping through the
entire collection. You could get the index position through a counter you
populate :

	Int counter = 0;
	foreach(ArrayList intEndFloor in PassengersOnCar) {

	// do stuff
	// if statement 
	PassengersOnCar.RemoveAt( counter );
	// end if
	Counter++; // increase counter by 1


or by using a for statement

	for(int I = 0; I < PassengersOnCar.Count; I++)
	{
	//do stuff
	PassengersOnCar.RemoveAt( I );


Hope this helps
Feel free to email me off list
	

Sincerely,
Robert Vreeland
Managing Partner 
StudioFramework 




More information about the thelist mailing list