Lamis,
Attached is a sample of what both Jay and I were explaining.
Jared
class Program
{
static void Main(string[] args)
{
Hashtable customersWithCheckingAccounts = new Hashtable();
Hashtable customersWithSavingsAccounts = new Hashtable();
Hashtable customersThatDontHaveBothCheckingAndSavings = new
Hashtable();
LoadAccounts(
customersWithCheckingAccounts,
customersWithSavingsAccounts
);
MyHashComparer comparer = new MyHashComparer();
PrintHeader("Default comparer action");
comparer.Compare(
customersWithCheckingAccounts,
customersWithSavingsAccounts);
PrintHeader("Difference table enumeration");
customersThatDontHaveBothCheckingAndSavings =
comparer.GetDifferences(
customersWithCheckingAccounts,
customersWithSavingsAccounts);
foreach (DictionaryEntry entry in
customersThatDontHaveBothCheckingAndSavings)
{
Console.WriteLine(((MyDataObject)entry.Value).ToSt ring());
}
Console.ReadLine();
}
static void PrintHeader(string message)
{
string sep = new string('*', 50);
Console.WriteLine();
Console.WriteLine(sep);
Console.WriteLine(message);
Console.WriteLine(sep);
Console.WriteLine();
}
static void LoadAccounts(Hashtable checking, Hashtable savings)
{
string customerName;
string accountNumber;
MyDataObject dataObject;
for (int i = 1; i < 30; i++)
{
if (i % 2 == 0)
{
customerName = string.Format("John {0}. Public",
GetInitial(i));
}
else
{
customerName = string.Format("Jane {0}. Public",
GetInitial(i));
}
accountNumber = string.Format("500000{0}", i);
dataObject = new MyDataObject(customerName, accountNumber);
checking.Add(dataObject.ToString(), dataObject);
if (i % 3 != 0)
{
savings.Add(dataObject.ToString(), dataObject);
}
}
}
private static string GetInitial(int i)
{
while (i 26)
{
i -= 26;
}
return new string((char)(64 + i), 1);
}
}
public sealed class MyHashComparer
{
delegate void MyDataObjectAction(MyDataObject dataObject);
private Hashtable _differences;
public void Compare(Hashtable a, Hashtable b)
{
// Default to print differences.
MyDataObjectAction action = new
MyDataObjectAction(PrintDifferences);
Compare(a, b, action);
}
private void Compare(Hashtable a, Hashtable b, MyDataObjectAction
action)
{
IEnumerator iter = a.GetEnumerator();
while (iter.MoveNext())
{
MyDataObject dataObject =
(MyDataObject)((DictionaryEntry)iter.Current).Valu e;
if (b.ContainsValue(dataObject) == false)
{
action.Invoke(dataObject);
}
}
}
public Hashtable GetDifferences(Hashtable a, Hashtable b)
{
_differences = new Hashtable();
MyDataObjectAction action = new
MyDataObjectAction(BuildDifferences);
Compare(a, b, action);
return _differences;
}
private void BuildDifferences(MyDataObject dataObject)
{
_differences.Add(dataObject.ToString(), dataObject);
}
private static void PrintDifferences(MyDataObject dataObject)
{
Console.WriteLine(dataObject.ToString());
}
}
public class MyDataObject
{
private MyDataObject() { }
public MyDataObject(string name, string value)
{
_name = name;
_value = value;
}
private string _name;
public string Name
{
get
{
if (_name == null)
{
return "<Null>";
}
return _name;
}
set
{
_name = value;
}
}
private string _value;
public string Value
{
get
{
if (_value == null)
{
return "<Null>";
}
return _value;
}
set { _value = value; }
}
public override bool Equals(object obj)
{
if (obj is MyDataObject)
{
MyDataObject dataObject = (MyDataObject)obj;
return (
this._name.Equals(dataObject.Name) &&
this._value.Equals(dataObject.Value)
);
}
return false;
}
public override int GetHashCode()
{
return this.ToString().ToLower().GetHashCode();
}
public override string ToString()
{
return (
string.Format("{0}:{1}", this.Name, this.Value)
);
}
}
"Lamis" wrote:
hi,
ok, but in this case I will only know if there are equal or not.... I need
more than that.. I have to print out all the defferences between the 2
haschtables.. all diffrences in objects.
--
LZ
"Lamis" wrote:
Hi,
what is the best way to compare 2 haschtables contatining objects. the
objects has 2 property, name & value. I need to print out the differences
--
LZ