Can't remove from ArrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pipetawer
    New Member
    • Sep 2007
    • 1

    Can't remove from ArrayList

    Greetings,

    I am having some kind of trouble with some code. I have the following object
    Code:
    using System;
    using System.Collections;
    
    namespace ImageServerCleanup
    {
    	public class DiffList : CollectionBase
    	{
    		public Diff this[int index]
    		{
    			get 
    			{ 
    				return ((Diff)(List[index])); 
    			}
    			set
    			{
    				List[index] = value;
    			}
    		}
    
    		public int Add( Diff value )		
    		{			
    			return( List.Add( value ) );		
    		}		
    		
    		public int IndexOf( Diff value )	
    		{			
    			return( List.IndexOf( value ) );		
    		}		
    		
    		public void Insert( int index, Diff value )			
    		{			
    			List.Insert( index, value );		
    		}		
    		
    		public void Remove( Diff value )  		
    		{			
    			List.Remove( value );		
    		}		
    		
    		public bool Contains( Diff value )  		
    		{					
    			return( List.Contains( value ) );
    		}
    
    		public class Diff
    		{
    			private string _path;
    			private string _filename;
    
    			public Diff(string path, string filename)
    			{
    				this._path = path;
    				this._filename = filename;
    			}
    
    			public Diff()
    			{
    
    			}
    
    			public string Path
    			{
    				get 
    				{
    					return _path;
    				}
    				set
    				{
    					_path = value;
    				}
    			}
    
    			public string Filename
    			{
    				get
    				{
    					return _filename;
    				}
    				set
    				{
    					_filename = value;
    				}
    			}
    		}
    	}
    }
    My code is:
    Code:
    DiffList ArrayA = new DiffList();
    for(int row = 0; row < rowCount; row++) 
    {
    	DiffList.Diff entry = new DiffList.Diff();
    	entry.Path = data1.ToString().ToLower();
    	entry.Filename = data2.ToString().ToLower(); 
    	ArrayA.Add(entry);
    }
    
    DiffList ArrayB = new DiffList();
    for(int row = 0; row < rowCount; row++) 
    {
    	DiffList.Diff entry = new DiffList.Diff();
    	entry.Path = data3.ToString().ToLower();
    	entry.Filename = data4.ToString().ToLower(); 
    	ArrayB.Add(entry);
    }
    
    foreach (DiffList.Diff fileToRemove in ArrayA)
    {
    	ArrayB.Remove(fileToRemove);
    }
    What I am doing is filling two two Arrays of this objects and then trying to substract ArrayA from ArrayB, but it doesn't. Anyone knows why?

    Thanks
  • ninjastar85
    New Member
    • May 2007
    • 2

    #2
    try using a for loop instead of for each.

    I think for lists if you use foreach it locks the values so you can't remove or edit it. Same thing happens when you are using gerneric list or data dictionary, if you use foreach it wont let you change the values..but for loop works

    Comment

    Working...