Click here to Skip to main content
15,896,329 members
Home / Discussions / C#
   

C#

 
GeneralRe: Books Pin
David Stone9-Jun-03 8:03
sitebuilderDavid Stone9-Jun-03 8:03 
GeneralRe: Books Pin
Makover9-Jun-03 11:09
Makover9-Jun-03 11:09 
GeneralRe: Books Pin
KingTermite10-Jun-03 3:11
KingTermite10-Jun-03 3:11 
Generalaccessing GUI Pin
stonee747-Jun-03 23:00
stonee747-Jun-03 23:00 
GeneralRe: accessing GUI Pin
Rocky Moore8-Jun-03 5:41
Rocky Moore8-Jun-03 5:41 
GeneralRe: accessing GUI Pin
stonee748-Jun-03 10:06
stonee748-Jun-03 10:06 
GeneralRe: accessing GUI Pin
Rocky Moore8-Jun-03 17:57
Rocky Moore8-Jun-03 17:57 
QuestionIs there a good way to do this? Pin
dratti7-Jun-03 22:39
dratti7-Jun-03 22:39 
Say I've got some type of collection (usually an ArrayList), and I need to itterate through it, call some methods, and possibly remove one or more of the elements. You cant modify a collection while enumerating through it. If its an Array I could access it with an index variable, but then I need to always check if the size has changed and adjust for that (which is incredibly tricky because you dont know which element was removed and whether you need to readjust your index value or not). Are there any elegant ways of doing this?


Heres what I mean
<br />
for(int i=0; i < m_objects.Count; i++)<br />
{<br />
       PhysicsObject obj2 = (PhysicsObject)m_objects[i];<br />
<br />
       if(obj2 == obj)<br />
	  continue;<br />
<br />
	if(rect.IntersectsWith(obj2.getBoundingBox()) || rect.Contains(obj2.getBoundingBox()))<br />
	{<br />
		//collision<br />
	        obj.onCollision(obj2);   //This could remove an object from m_objects<br />
		obj2.onCollision(obj);   //This could remove an object from m_objects<br />
	}<br />
  <br />
        //If an object was removed m_objects.Count is one less. If the object that was removed had<br />
        //already been enumerated, we need to do the same index again (all elements shift down) <br />
        //if it hasnt been enumerated, we dont do anything to i.<br />
}<br />


This may be more a design problem. I can think of a few ways to hack around this (dont delete objects inside of onCollision, just put them in a queue to be deleted after all collision detections are done), but Im hoping theres an elegant way to itterate through a collection and be able to remove elements without stepping on your toes.

Thanks for any help
Dave Ratti
AnswerRe: Is there a good way to do this? Pin
Iulian Serban8-Jun-03 6:04
Iulian Serban8-Jun-03 6:04 
AnswerRe: Is there a good way to do this? Pin
KingTermite8-Jun-03 8:39
KingTermite8-Jun-03 8:39 
GeneralDistributing C# .NET programs... major headache. Pin
EvilDingo7-Jun-03 20:13
EvilDingo7-Jun-03 20:13 
GeneralRe: Distributing C# .NET programs... major headache. Pin
leppie7-Jun-03 22:50
leppie7-Jun-03 22:50 
GeneralRe: Distributing C# .NET programs... major headache. Pin
EvilDingo8-Jun-03 0:42
EvilDingo8-Jun-03 0:42 
GeneralRe: Distributing C# .NET programs... major headache. Pin
leppie8-Jun-03 1:04
leppie8-Jun-03 1:04 
GeneralRe: Distributing C# .NET programs... major headache. Pin
Steven Behnke10-Jun-03 12:58
Steven Behnke10-Jun-03 12:58 
QuestionLable auto resize vertically to show hidden text? Pin
Weiye Chen7-Jun-03 18:44
Weiye Chen7-Jun-03 18:44 
GeneralI encountered an System.Net.Sockets.SocketException, but dunno why it happened Pin
viperxp7-Jun-03 17:39
viperxp7-Jun-03 17:39 
GeneralRe: I encountered an System.Net.Sockets.SocketException, but dunno why it happened Pin
leppie7-Jun-03 22:38
leppie7-Jun-03 22:38 
GeneralRe: I encountered an System.Net.Sockets.SocketException, but dunno why it happened Pin
viperxp15-Jun-03 5:54
viperxp15-Jun-03 5:54 
QuestionHow to check... Pin
Kant7-Jun-03 17:36
Kant7-Jun-03 17:36 
AnswerRe: How to check... Pin
leppie7-Jun-03 22:35
leppie7-Jun-03 22:35 
GeneralRe: How to check... Pin
Kant8-Jun-03 4:21
Kant8-Jun-03 4:21 
GeneralRe: How to check... Pin
shaunAustin9-Jun-03 0:46
shaunAustin9-Jun-03 0:46 
GeneralRe: How to check... Pin
Kant9-Jun-03 5:03
Kant9-Jun-03 5:03 
QuestionHow to create a 'copy' and 'paste' feature for selected cell ranges Pin
sleepycat7-Jun-03 15:40
sleepycat7-Jun-03 15:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.