SP wrote:
You need to use a hashtable and create a key from the 2 properties that
uniquely identifiesthe object. Check if the hashtable contains the key and
add the object if it doesn't. Dictionary implements a hashtable internally
and you get type safety as it uses generics. What were you currently doing?
It's a small app to consolidate MSN chat logs. The objects I need to
keep in memory are Message objects, and there's a MessageCollection
class already, but it doesn't implement any interface for collections.
Message objects are added to MessageCollection one file at a time with
AddChatFile(). That's where I run into this problem of checking which
messages are already added.
public void AddChatFile(string file)
{
ChatFileReader chatFileReader = new ChatFileReader();
chatFileReader.Load(file);
Message[] messagesInFile = chatFileReader.Messages;
foreach (Message message in messagesInFile)
{
if (!IsAlreadyAdded(message))
this.messages.Add(message);
}
}
private bool IsAlreadyAdded(Message message)
{
if (this.messages == null) return false;
foreach (Message m in this.messages)
{
if (m.DateTime == message.DateTime &&
m.From == message.From)
{
return true;
}
}
return false;
}
Gustaf