by

A Hashtable-like ArrayList in C#

I started working on WebSync again tonight. I’ve decided to make two major changes to jump start me on the project again: (a) streamline the server-side API to make it more file-manipulation like, (b) instead of treating the file paths as the identifier for a file I issue a GUID to track the file as it moves around (unique of its location, name and other metadata). The former makes the client much simpler and the latter makes the sync model eaiser to comprehend.

In any case, since I’m using GUIDs to track the objects I wanted to use a Hashtable to store all the file data and XML serialze it. Unfortunatly, that doesn’t work. Why, I’m not quite sure. .NET barfs and says it can’t touch the class because it implements IDictionary. So, I decided to make a Hashtable-like ArrayList. The only reason I wanted to use the Hashtable was to get the indexer anyway, so I just subclassed ArrayList. The code below is type checked for a “FileInfo” class, which has a member FileID that is a GUID (what I mentioned earlier for file tracking) which serves as key to make this Hashtable-like. Here is the C# code; it’s not optimized or elegant, but it gets the job done and makes me fall in love with OOP all over again.

public class FileArrayList : ArrayList
{
   public override int Add(object Value)
   {
      if(Value.GetType() != typeof(FileInfo))
         throw new Exception(“Bad!”); 
      
if(this[((FileInfo)Value).FileID] != null)
         
throw new Exception(“Already in this”);
      
return base.Add (Value);
   }
   public FileInfo this[Guid G]
   {
      get
      {
         foreach(FileInfo f in this)
         {
            if(f.FileID == G)
            {
               return f;
            }
         }
         return null; 
      }
      set
      {
         FileInfo searchInfo =
null;
         foreach(FileInfo f in this)
         {
            if(f.FileID == G)
            { 
               searchInfo =
value;
               break;
            } 
         }
         if(searchInfo != null)
         {
            this.Remove(searchInfo);
            this.Add(value);
         }
      }
   }
}