473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

compare 2 haschtables

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
Jul 28 '06 #1
4 6643
You have to override the Object.Equals method. Attached is an example using
IEqualityComparer.

http://msdn2.microsoft.com/en-us/lib...ay(d=ide).aspx

"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
Jul 28 '06 #2
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
Jul 28 '06 #3
LZ,
I would probably define a 3rd hashtable contains a "union" of the first two
hash tables. This "union" would be a special object that contains name,
value1 (from hash 1) and value2 (from hash 2).

When adding the first hash table I would update the value1 properties, when
adding the second hash table I would update the value2 properties.

Then I would remove entries where value1 = value2.

I would then print out all the entries in this 3rd hashtable...

If that made no sense? I will try to post an example this evening.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Lamis" <La***@discussions.microsoft.comwrote in message
news:8B**********************************@microsof t.com...
| 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
Jul 28 '06 #4
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
Jul 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte array to another? This is my current code //...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
1
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
5
by: antani | last post by:
I need to implement a function with a argument that is a compare function. This compare function must be several for every necessity. For example , I would like a compare function to analyze...
26
by: neha_chhatre | last post by:
can anybody tell me how to compare two float values say for example t and check are two variables declared float how to compare t and check please help me as soon as possible
1
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.