473,386 Members | 1,699 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,386 software developers and data experts.

Compare objects

I have 2 objects that have the same complex structure and I would like
to know if a member has change and which one(s).

For example:

[Serializable]
public struct Person
{
public long m_nID;
public string m_strPrenom;
public string m_strNom;
}

If I have p1 and p2 which are Person, I would like a method like
p1.theMethod(p2) that returns an array of objects with the member(s)
and the value(s) where the values are different.

My structure is big, so I don't know if any method already exists in
dotnet?

Thanks,
Francois
Nov 16 '05 #1
2 2215
Hi Frank,

I propose You to write additional structure e.g.

public struct PersonDetailDifference {
public int IdDifference;

public int PrenomDifference;

public int NomDifference;

// and continue...
}

Results of each fields should correspond with
IComparable.Compare(...) results of two Person
field values.

Person should have a constant structure so i
propose to use another strucure as a result
of comparison.

The good practise is to use the properties
instead of fieds for public access.

Regards

Marcin
I have 2 objects that have the same complex structure and I would like
to know if a member has change and which one(s).

For example:

[Serializable]
public struct Person
{
public long m_nID;
public string m_strPrenom;
public string m_strNom;
}

If I have p1 and p2 which are Person, I would like a method like
p1.theMethod(p2) that returns an array of objects with the member(s)
and the value(s) where the values are different.

My structure is big, so I don't know if any method already exists in
dotnet?

Thanks,
Francois

Nov 16 '05 #2
There is no "out-ouf-the-box" solution for this (to the best of my
knowledge).
But you can automate this task using reflection: Get the metadata of your
type, loop over it's fields/properties and compare each of them, put the
different ones in an array.

Look at Object.GetType; Type.GetFields/GetProperties, FieldInfo.GetValue,
PropertyInfo.GetValue.

Niki

"Frank" <fr*************@bluemail.ch> wrote in
news:39**************************@posting.google.c om...
I have 2 objects that have the same complex structure and I would like
to know if a member has change and which one(s).

For example:

[Serializable]
public struct Person
{
public long m_nID;
public string m_strPrenom;
public string m_strNom;
}

If I have p1 and p2 which are Person, I would like a method like
p1.theMethod(p2) that returns an array of objects with the member(s)
and the value(s) where the values are different.

My structure is big, so I don't know if any method already exists in
dotnet?

Thanks,
Francois

Nov 16 '05 #3

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

Similar topics

12
by: Helmut Jarausch | last post by:
Hi, what does Python do if two objects aren't comparable (to my opinion) If I've understood "Python in a Nutschell" correctly it should raise an exception but it doesn't do for me. Here are...
4
by: Nicolas Fleury | last post by:
Hi everyone, Is there a way to compare recursively two objects (compare their members recursively)? I'm only interested in equality or non-equality (no need for lower-than...). Thx and...
3
by: Stephen | last post by:
I have to write a .Net application which can compare SQL Databases including things like: - DB structure, PK's, FK's, indexes and types of indexes i.e. should be able to detect if the same index...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
3
by: vertigo | last post by:
Hello I have std::map object and i want to have randomly sorted objects in it. I tried to: std::map<int,RandomCompare> myobject; and: struct RandomCompare{ bool operator(int i1, int i2){
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
4
by: Lamis | last post by:
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
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.