468,253 Members | 1,237 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

Need to Compare 2 Copies of Complex Data Structure

Hi

I have a Container that is an an Array List of Class
Each ArrayList element can be the class or a another ArrayList of Class
So there the ArrayList could look like

Element 1 - Class
Element 2 - Class
Element 1 - ArrayList Of Class
Element 1 - Class
Element 2 - Class
Element 3 - Class
Element 4 - Class

I put elements on buy recursively going thru elements

Now I need to compare 2 of these Items and determine the difference

Any ideas on how to best achieve this ???

I was thinking of doing it recursively but I need to keep the 2 items at the
same point to compare element by elmement and wasn't sure how to do that ???

Thanks
Oct 8 '08 #1
5 2172
You need to establish the rules of the comparison. Then implement IComparable
on the class and possibly create a subclass of arraylist to then implement
IComparable on there.
If you give a brief description of the rules for comparison then I would be
able to give you a bit of sample code.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"sippyuconn" wrote:
Hi

I have a Container that is an an Array List of Class
Each ArrayList element can be the class or a another ArrayList of Class
So there the ArrayList could look like

Element 1 - Class
Element 2 - Class
Element 1 - ArrayList Of Class
Element 1 - Class
Element 2 - Class
Element 3 - Class
Element 4 - Class

I put elements on buy recursively going thru elements

Now I need to compare 2 of these Items and determine the difference

Any ideas on how to best achieve this ???

I was thinking of doing it recursively but I need to keep the 2 items at the
same point to compare element by elmement and wasn't sure how to do that ???

Thanks
Oct 8 '08 #2
On Oct 8, 7:26*am, sippyuconn <sippyuc...@newsgroup.nospamwrote:
Hi

I have a Container that is an an Array List of Class
Each ArrayList element can be the class or a another ArrayList of Class
So there the ArrayList could look like

Element 1 - Class
Element 2 - Class
Element 1 - ArrayList Of Class
* * * * * * * * * * Element 1 - Class
* * * * * * * * * * Element 2 - Class
Element 3 - Class
Element 4 - Class

I put elements on buy recursively going thru elements

Now I need to compare 2 of these Items and determine the difference

Any ideas on how to best achieve this ???

I was thinking of doing it recursively but I need to keep the 2 items at the
same point to compare element by elmement and wasn't sure how to do that ???

Thanks
I really do not understand your question. However from the subject
line I think you wanted to compare two different datastructures?

If so, Please explore IComparable , overloading == and also explore
Equals method of object, which can give you a good knowledge how to
procede with your issue.

-Cnu
Oct 8 '08 #3
Hi sippyuconn,

Thank you for contacting Microsoft Managed Newsgroup Support Service, I'm
Zhi-Xin and I'm assigned to help you with this issue.

First I would like to confirm my understanding with you. Do you mean you
have two ArrayList of a custom class, and you want to compare these two
lists element by element to determine the difference? Are the two
ArrayList of the same length?

I look forward to hearing from you.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 9 '08 #4
Hi

Yes they are the same structure/length only values in elements maybe differnt
I basically clone the original object then make change a value or 2 then I
need to find difference

Here example

public class Record
{
public bool IsField = true;
public bool IsSubCollection = false;

public string value = "";
}

public class MainCollection : ArrayList
{

}

public class SubCollection : ArrayList
{

public bool IsField = false;
public bool IsSubCollection = true;
}

MainCollection coll = new MainCollection();
SubCollection subcoll = new SubCollection();
for(int i = 0; i < 5; i++)
{
Record rec = new Record();
subcoll.Add(rec);
}

for (int i = 0; i < 5; i++)
{
Record rec = new Record();
coll.Add(rec);
}

coll.Add(subcoll);

Thanks
"Zhi-Xin Ye [MSFT]" wrote:
Hi sippyuconn,

Thank you for contacting Microsoft Managed Newsgroup Support Service, I'm
Zhi-Xin and I'm assigned to help you with this issue.

First I would like to confirm my understanding with you. Do you mean you
have two ArrayList of a custom class, and you want to compare these two
lists element by element to determine the difference? Are the two
ArrayList of the same length?

I look forward to hearing from you.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 10 '08 #5
Hi sippyuconn,

Do you mean you have two list of MainCollection, and their structure and
length are of the same?
If so, you can implement the IComparable interface on the Record class,
write your comparation rule in the CompareTo() method.
Then you can write a compare function to loop through the lists to compare
the elements:

- If the element is Record object, call the CompareTo() method to compare
the differences;
- If the element is SubCollection object, convert the element to
SubCollection object and call the compare function recursively;

I write the following sample for your information:

namespace Sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
MainCollection coll = new MainCollection();
SubCollection subcoll = new SubCollection();

//======= Main collection 1 =====
for (int i = 0; i < 5; i++)
{
Record rec = new Record();
subcoll.Add(rec);
}

for (int i = 0; i < 5; i++)
{
Record rec = new Record();
coll.Add(rec);
}

coll.Add(subcoll);

//======= Main collection 2 =====

MainCollection coll2 = new MainCollection();
SubCollection subcoll2 = new SubCollection();
for (int i = 0; i < 5; i++)
{
Record rec = new Record();
subcoll2.Add(rec);
}

for (int i = 0; i < 5; i++)
{
Record rec = new Record();
coll2.Add(rec);
}

coll2.Add(subcoll2);

//==== Change some element ====

Record r = (Record)coll2[0];
r.value = "test";

((Record)((SubCollection)coll2[5])[2]).value = "something";

CompareList(coll, coll2);
}

ArrayList levels = new ArrayList();

public void CompareList(ArrayList arr1, ArrayList arr2)
{
for (int j = 0; j < arr1.Count; j++)
{
if (arr1[j] is Record)
{
Record rd1 = (Record)arr1[j];
Record rd2 = (Record)arr2[j];
if (rd1.CompareTo(rd2) != 0)
{
levels.Add(j);

string pos = "";
foreach (int ls in levels)
{
pos += ls.ToString() + "-";
}

Console.WriteLine("Elements at position: {0} are
different. The values are \"{1}\" vs \"{2}\"",
pos, rd1.value, rd2.value);

levels.Remove(j);
}
}
if (arr1[j] is SubCollection)
{
levels.Add(j);
SubCollection sub1 = (SubCollection)arr1[j];
SubCollection sub2 = (SubCollection)arr2[j];
//If the element is SubCollection object, call the
compare function recursively;
CompareList(sub1, sub2);
levels.Remove(j);
}
}
}
}
public class Record : IComparable
{
public string value = "";

public int CompareTo(object obj)
{
Record r = obj as Record;
if (r != null)
{
return this.value.CompareTo(r.value);
}
else
{
throw new ArgumentException("Object is not Record!");
}
}
}

public class MainCollection : ArrayList
{
}

public class SubCollection : ArrayList
{
}
}
The output of the sample is as follows:

Elements at position: 0- are different. The values are "" vs "test"
Elements at position: 5-2- are different. The values are "" vs "something"

If any of my solution is not clear, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 10 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Carl Bevil | last post: by
116 posts views Thread by Mike MacSween | last post: by
2 posts views Thread by Chris | last post: by
1 post views Thread by Frank | last post: by
3 posts views Thread by Kiran B. | last post: by
5 posts views Thread by fade | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.