473,386 Members | 2,050 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 two instance of a class

Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.

Nov 16 '05 #1
17 8198
bengamin wrote:
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.


I have been trying to find out the same thing.
From what I can see, if you overrride == in your class then you should
also override Equals, != and GetHashCode.
So that they all exhibit the same behaviour.

Since I might want to test for reference equality in one case and say
check the ID property in another case I would like to override == to
check for ID equality but from what I understand thats not a good idea
as other languages (VB) does not have this operator.

You might want to define an interface that would do a property check on
both instances to determine whether they are equal.

Sorry I cannot be more decisive, I would like to see others views :)

Cheers

--
JB
Nov 16 '05 #2
Recommended practice is that operator "==" and method
"Object.Equals(object)" should behave the same.
If you override Equal you should also override GetHashCode.

You need to bear in mind whether or not you will ever need to compare 2
references (which is the default behaviour) i.e. that refer to the same
instance.

In most cases I think it is better to implement a specific method to compare
reference type instances - value comparison is often a business/application
specific function, and it may be misleading to some developers if you
override default language functionality.

Here's a useful article on MSDN:
http://msdn.microsoft.com/library/de...lsoperator.asp

Richard.

"bengamin" <sa***@langchao.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.

Nov 16 '05 #3
Sorry but I've gotten curious because of these replies. I don't seem to
understand the question maybe?

I thought what he meant was just that if you have let's say, the following
class:

class A
{
private int _i;

public int i
{
get
{ return this._i; }
set
{ this._i = value; }
}
}

and A a1 = new A(); A a2 = new A();
then a1.i == a2.i would simply return true if they have the same value? That
was the question right? Why would you need to overload the operator for
that?
"richlm" <ri*****@h0tmai1.com> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...
Recommended practice is that operator "==" and method
"Object.Equals(object)" should behave the same.
If you override Equal you should also override GetHashCode.

You need to bear in mind whether or not you will ever need to compare 2
references (which is the default behaviour) i.e. that refer to the same
instance.

In most cases I think it is better to implement a specific method to compare reference type instances - value comparison is often a business/application specific function, and it may be misleading to some developers if you
override default language functionality.

Here's a useful article on MSDN:
http://msdn.microsoft.com/library/de...lsoperator.asp
Richard.

"bengamin" <sa***@langchao.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.


Nov 16 '05 #4
Hi,

Good practise is to implement "IComparable" interface.

int CompareTo(Object obj)
- it should return zero if "obj" is equal to base object.

Then you have to (easily) override the "Equals()", as follow:

public override bool Equals(Object obj) {
return (this.CompareTo(obj)==0);
}

Cheers

Marcin
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.

Nov 16 '05 #5
Hi Razzie,

Don't you think that there can be more properties than
one or two?

e.g.
// A Person class, that have: FirstName, LastName, Age, Gender.
Person person1=new Person();
Person person2=new Person();

if( person1.FirstName==person2.FirstName
&& person1.LastName==person2.LastName
&& person1.Age==person2.Age
&& person1.Gender==person2.Gender ) {
//...
}

It looks very complicated. Don't you think?

Regards

Marcin

Sorry but I've gotten curious because of these replies. I don't seem to
understand the question maybe?

I thought what he meant was just that if you have let's say, the following
class:

class A
{
private int _i;

public int i
{
get
{ return this._i; }
set
{ this._i = value; }
}
}

and A a1 = new A(); A a2 = new A();
then a1.i == a2.i would simply return true if they have the same value? That
was the question right? Why would you need to overload the operator for
that?
"richlm" <ri*****@h0tmai1.com> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...
Recommended practice is that operator "==" and method
"Object.Equals(object)" should behave the same.
If you override Equal you should also override GetHashCode.

You need to bear in mind whether or not you will ever need to compare 2
references (which is the default behaviour) i.e. that refer to the same
instance.

In most cases I think it is better to implement a specific method to


compare
reference type instances - value comparison is often a


business/application
specific function, and it may be misleading to some developers if you
override default language functionality.

Here's a useful article on MSDN:


http://msdn.microsoft.com/library/de...lsoperator.asp
Richard.

"bengamin" <sa***@langchao.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.



Nov 16 '05 #6
Hi,

== overloading should be the way to go, now according to MSDN you also need
to overload != but nothing more, it saids nothing about Equals or
GetHashCode.

If you overload the default meaning of the == operator you will not longer
compare references , if you want do so you could do it casting both operands
to Object, like this:
(object)instance1 == (object) instance2
It should work.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"The Last Gunslinger" <jb******@yahoo.com> wrote in message
news:6X*******************@news-server.bigpond.net.au...
bengamin wrote:
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.


I have been trying to find out the same thing.
From what I can see, if you overrride == in your class then you should
also override Equals, != and GetHashCode.
So that they all exhibit the same behaviour.

Since I might want to test for reference equality in one case and say
check the ID property in another case I would like to override == to
check for ID equality but from what I understand thats not a good idea
as other languages (VB) does not have this operator.

You might want to define an interface that would do a property check on
both instances to determine whether they are equal.

Sorry I cannot be more decisive, I would like to see others views :)

Cheers

--
JB

Nov 16 '05 #7
ah well, he wrote value and not values... :)

"Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:ce*********@atlantis.news.tpi.pl...
Hi Razzie,

Don't you think that there can be more properties than
one or two?

e.g.
// A Person class, that have: FirstName, LastName, Age, Gender.
Person person1=new Person();
Person person2=new Person();

if( person1.FirstName==person2.FirstName
&& person1.LastName==person2.LastName
&& person1.Age==person2.Age
&& person1.Gender==person2.Gender ) {
//...
}

It looks very complicated. Don't you think?

Regards

Marcin

Sorry but I've gotten curious because of these replies. I don't seem to
understand the question maybe?

I thought what he meant was just that if you have let's say, the following class:

class A
{
private int _i;

public int i
{
get
{ return this._i; }
set
{ this._i = value; }
}
}

and A a1 = new A(); A a2 = new A();
then a1.i == a2.i would simply return true if they have the same value? That was the question right? Why would you need to overload the operator for
that?
"richlm" <ri*****@h0tmai1.com> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...
Recommended practice is that operator "==" and method
"Object.Equals(object)" should behave the same.
If you override Equal you should also override GetHashCode.

You need to bear in mind whether or not you will ever need to compare 2
references (which is the default behaviour) i.e. that refer to the same
instance.

In most cases I think it is better to implement a specific method to


compare
reference type instances - value comparison is often a


business/application
specific function, and it may be misleading to some developers if you
override default language functionality.

Here's a useful article on MSDN:


http://msdn.microsoft.com/library/de...lsoperator.asp
Richard.

"bengamin" <sa***@langchao.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...

Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.



Nov 16 '05 #8
Yes, question just as you describe

since i want to build a command function for all class,and i don't know how
to do,so i thought about overload "=="

"Razzie" <ra****@quicknet.nl> дÈëÓʼþ
news:ex**************@TK2MSFTNGP12.phx.gbl...
Sorry but I've gotten curious because of these replies. I don't seem to
understand the question maybe?

I thought what he meant was just that if you have let's say, the following
class:

class A
{
private int _i;

public int i
{
get
{ return this._i; }
set
{ this._i = value; }
}
}

and A a1 = new A(); A a2 = new A();
then a1.i == a2.i would simply return true if they have the same value? That was the question right? Why would you need to overload the operator for
that?
"richlm" <ri*****@h0tmai1.com> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...
Recommended practice is that operator "==" and method
"Object.Equals(object)" should behave the same.
If you override Equal you should also override GetHashCode.

You need to bear in mind whether or not you will ever need to compare 2
references (which is the default behaviour) i.e. that refer to the same
instance.

In most cases I think it is better to implement a specific method to

compare
reference type instances - value comparison is often a

business/application
specific function, and it may be misleading to some developers if you
override default language functionality.

Here's a useful article on MSDN:

http://msdn.microsoft.com/library/de...lsoperator.asp

Richard.

"bengamin" <sa***@langchao.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.



Nov 16 '05 #9
thank you very much!

"The Last Gunslinger" <jb******@yahoo.com> ????
news:6X*******************@news-server.bigpond.net.au...
bengamin wrote:
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.


I have been trying to find out the same thing.
From what I can see, if you overrride == in your class then you should
also override Equals, != and GetHashCode.
So that they all exhibit the same behaviour.

Since I might want to test for reference equality in one case and say
check the ID property in another case I would like to override == to
check for ID equality but from what I understand thats not a good idea
as other languages (VB) does not have this operator.

You might want to define an interface that would do a property check on
both instances to determine whether they are equal.

Sorry I cannot be more decisive, I would like to see others views :)

Cheers

--
JB

Nov 16 '05 #10
Thank you for your help!
Can you give us some example?

"Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> ????
news:ce**********@atlantis.news.tpi.pl...
Hi,

Good practise is to implement "IComparable" interface.

int CompareTo(Object obj)
- it should return zero if "obj" is equal to base object.

Then you have to (easily) override the "Equals()", as follow:

public override bool Equals(Object obj) {
return (this.CompareTo(obj)==0);
}

Cheers

Marcin
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.


Nov 16 '05 #11
Unless you want complete value semantics (like the string class has), you
shouldn't override the == operator.

Can't you just have a special equality method on the class:

class X
{
private int myProperty;
public int MyProperty { get { return this.myProperty } }

bool IsValueEqualTo(X otherX)
{
return this.MyProperty == otherX.MyProperty;
}
}

Christian
"bengamin" <sa***@langchao.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.

Nov 16 '05 #12
Hi bengamin,

Here is your sample:

public class Person
: IComparable
{
private int age;
private string lastName;
private string firstName;
private bool maleGender;

public int CompareTo(object obj) {
Person secondPerson=obj as Person;
if( secondPerson==null ) {
throw new ArgumentNullException("obj");
}
int result;
result=(age-obj.age);
if( result!=0 ) {
return result;
}
result=lastName.CompareTo(obj.lastName);
if( result!=0 ) {
return result;
}
result=firstName.CompareTo(obj.firstName);
if( result!=0 ) {
return result;
}
result=maleGender.CompareTo(obj.maleGender);
return result;
}

}

Cheers!

Marcin
Thank you for your help!
Can you give us some example?

"Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> ????
news:ce**********@atlantis.news.tpi.pl...
Hi,

Good practise is to implement "IComparable" interface.

int CompareTo(Object obj)
- it should return zero if "obj" is equal to base object.

Then you have to (easily) override the "Equals()", as follow:

public override bool Equals(Object obj) {
return (this.CompareTo(obj)==0);
}

Cheers

Marcin

Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.



Nov 16 '05 #13
Hi,Marcin

Thany you !

The example you give have a presupposition/precondition that we must know
the properties ,but the fact is we don't know the properties.

Today i found a new way to implement the function
This is the example code:

Class1 a = new Class1();
Class1 b = new Class1();
XmlSerializer formatter = new XmlSerializer(typeof(Class1));
Byte[] buff = new Byte[1024];
Stream stream = new MemoryStream(buff);
formatter.Serialize(stream, a);
Byte[] buff1 = new Byte[1024];
Stream stream1 = new MemoryStream(buff1);
formatter.Serialize(stream1, b);

for(int i = 0 ; i < buff.Length ; i ++ )
{
if (buff[i] != buff1[i])
{
label1.Text = "false";
return;
}
}
label1.Text = "true";
But there is some problem :
1, HashTable can not be xmlserializer;
2, The sample class i used for example is ok ,but when turn to the class of
our business layer,
there come an error when step into "Stream stream = new
MemoryStream(buff);"
I don't know why ,is somebody can give me some help?

Thank you!

"Marcin Grzêbski" <mg*******@taxussi.spam.com.stop.pl> ????
news:ce***********@mamut.aster.pl...
Hi bengamin,

Here is your sample:

public class Person
: IComparable
{
private int age;
private string lastName;
private string firstName;
private bool maleGender;

public int CompareTo(object obj) {
Person secondPerson=obj as Person;
if( secondPerson==null ) {
throw new ArgumentNullException("obj");
}
int result;
result=(age-obj.age);
if( result!=0 ) {
return result;
}
result=lastName.CompareTo(obj.lastName);
if( result!=0 ) {
return result;
}
result=firstName.CompareTo(obj.firstName);
if( result!=0 ) {
return result;
}
result=maleGender.CompareTo(obj.maleGender);
return result;
}

}

Cheers!

Marcin
Thank you for your help!
Can you give us some example?

"Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> ????
news:ce**********@atlantis.news.tpi.pl...
Hi,

Good practise is to implement "IComparable" interface.

int CompareTo(Object obj)
- it should return zero if "obj" is equal to base object.

Then you have to (easily) override the "Equals()", as follow:

public override bool Equals(Object obj) {
return (this.CompareTo(obj)==0);
}

Cheers

Marcin
Hi,

I have a C# class and two instance of the class;

the class have some property.

I want to compare the property value of the two instance

How should i do? override == ? use delegate ?

I am sorry ,my english was poor.


Nov 16 '05 #14
If you really want to do byte comparison, you would be better off using
BinaryFormatter.
(Note your XmlSerializer only takes the public properties). See Compare1
below.

Another good solution where you don't know the properties is to use
reflection. See Compare2 below - much simpler.

(sample code for illustration - not tested...)
*************

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;

namespace ClassLibrary1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
int i = 0;
string s = "";
object o = null;

public Class1()
{
}

public static bool Compare1( Class1 c1, Class1 c2 )
{
BinaryFormatter f1 = new BinaryFormatter();
MemoryStream s1 = new MemoryStream();
BinaryFormatter f2 = new BinaryFormatter();
MemoryStream s2 = new MemoryStream();

f1.Serialize(s1,c1);
f2.Serialize(s2,c2);

byte[] buf1 = s1.ToArray();
byte[] buf2 = s2.ToArray();

if (buf1.Length==buf2.Length)
return false;

for (int i=0; i<buf1.Length; i++)
{
if (buf1[i]!=buf2[i])
return false;
}
}

public static bool Compare2( Class1 c1, Class1 c2 )
{
Type t = c1.GetType();

foreach (PropertyInfo p in t.GetProperties())
{
if (p.GetValue(c1,null)!=p.GetValue(c2,null))
return false;
}
return true;
}
}
}

"bengamin" <sa***@langchao.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
Hi,Marcin

Thany you !

The example you give have a presupposition/precondition that we must know
the properties ,but the fact is we don't know the properties.

Today i found a new way to implement the function
This is the example code:

Class1 a = new Class1();
Class1 b = new Class1();
XmlSerializer formatter = new XmlSerializer(typeof(Class1));
Byte[] buff = new Byte[1024];
Stream stream = new MemoryStream(buff);
formatter.Serialize(stream, a);
Byte[] buff1 = new Byte[1024];
Stream stream1 = new MemoryStream(buff1);
formatter.Serialize(stream1, b);

for(int i = 0 ; i < buff.Length ; i ++ )
{
if (buff[i] != buff1[i])
{
label1.Text = "false";
return;
}
}
label1.Text = "true";
But there is some problem :
1, HashTable can not be xmlserializer;
2, The sample class i used for example is ok ,but when turn to the class of our business layer,
there come an error when step into "Stream stream = new
MemoryStream(buff);"
I don't know why ,is somebody can give me some help?

Thank you!

"Marcin Grzêbski" <mg*******@taxussi.spam.com.stop.pl> ????
news:ce***********@mamut.aster.pl...
Hi bengamin,

Here is your sample:

public class Person
: IComparable
{
private int age;
private string lastName;
private string firstName;
private bool maleGender;

public int CompareTo(object obj) {
Person secondPerson=obj as Person;
if( secondPerson==null ) {
throw new ArgumentNullException("obj");
}
int result;
result=(age-obj.age);
if( result!=0 ) {
return result;
}
result=lastName.CompareTo(obj.lastName);
if( result!=0 ) {
return result;
}
result=firstName.CompareTo(obj.firstName);
if( result!=0 ) {
return result;
}
result=maleGender.CompareTo(obj.maleGender);
return result;
}

}

Cheers!

Marcin
Thank you for your help!
Can you give us some example?

"Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> ????
news:ce**********@atlantis.news.tpi.pl...

>Hi,
>
>Good practise is to implement "IComparable" interface.
>
>int CompareTo(Object obj)
>- it should return zero if "obj" is equal to base object.
>
>Then you have to (easily) override the "Equals()", as follow:
>
>public override bool Equals(Object obj) {
> return (this.CompareTo(obj)==0);
>}
>
>Cheers
>
>Marcin
>
>
>>Hi,
>>
>>I have a C# class and two instance of the class;
>>
>>the class have some property.
>>
>>I want to compare the property value of the two instance
>>
>>How should i do? override == ? use delegate ?
>>
>>I am sorry ,my english was poor.
>
>


Nov 16 '05 #15
Hi again,
<snip>
The example you give have a presupposition/precondition that we must know
the properties ,but the fact is we don't know the properties.
I'm sorry. But i don't know how you want to compare something
that you don't know?
If you're desinging a class then you should know its properties.
You can not know its base class private (or internal) properties
but the others should be visible.

There's now universal method to implement IComparable interface.

If you want to write universal comparator, then you've got
a lot of work to do... :(
Today i found a new way to implement the function
This is the example code:

Class1 a = new Class1();
Class1 b = new Class1();
XmlSerializer formatter = new XmlSerializer(typeof(Class1));
Byte[] buff = new Byte[1024];
Stream stream = new MemoryStream(buff);
formatter.Serialize(stream, a);
Byte[] buff1 = new Byte[1024];
Stream stream1 = new MemoryStream(buff1);
formatter.Serialize(stream1, b);

for(int i = 0 ; i < buff.Length ; i ++ )
{
if (buff[i] != buff1[i])
{
label1.Text = "false";
return;
}
}
label1.Text = "true";
But there is some problem :
1, HashTable can not be xmlserializer;
2, The sample class i used for example is ok ,but when turn to the class of
our business layer,
there come an error when step into "Stream stream = new
MemoryStream(buff);"
If you want to serialize Hashtable then you can serialize its
keys and values (as a tables)
I don't know why ,is somebody can give me some help?

Thank you!


Cheers!

Marcin
Nov 16 '05 #16
Hi,

With Reflection ,i get the code like this
And it works very well when there is no Array ,ArrayList or HashTable etc...
I don't know how to deal with Array ,can you give me some advice ?
Thank You!

private bool Compare(Object a, Object b)
{
if (a != null && b != null)
{
Type t = a.GetType();

foreach(PropertyInfo p in t.GetProperties())
{
if (p.PropertyType.ToString() != "System.Object")
{
if ((!p.PropertyType.IsValueType) && (p.PropertyType.ToString() !=
"System.String"))
{
if( Compare(p.GetValue(a,null),p.GetValue(b,null)))
continue;
else
return false;
}
else
{
if (Convert.ToString(p.GetValue(a,null)) !=
Convert.ToString(p.GetValue(b,null)))
return false;
}
}
}
return true;
}
else
{
if (a == null && b == null)
return true;
return false;
}
}
"richlm" <ri*****@h0tmai1.com> дÈëÓʼþ
news:uc**************@TK2MSFTNGP11.phx.gbl...
If you really want to do byte comparison, you would be better off using
BinaryFormatter.
(Note your XmlSerializer only takes the public properties). See Compare1
below.

Another good solution where you don't know the properties is to use
reflection. See Compare2 below - much simpler.

(sample code for illustration - not tested...)
*************

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;

namespace ClassLibrary1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
int i = 0;
string s = "";
object o = null;

public Class1()
{
}

public static bool Compare1( Class1 c1, Class1 c2 )
{
BinaryFormatter f1 = new BinaryFormatter();
MemoryStream s1 = new MemoryStream();
BinaryFormatter f2 = new BinaryFormatter();
MemoryStream s2 = new MemoryStream();

f1.Serialize(s1,c1);
f2.Serialize(s2,c2);

byte[] buf1 = s1.ToArray();
byte[] buf2 = s2.ToArray();

if (buf1.Length==buf2.Length)
return false;

for (int i=0; i<buf1.Length; i++)
{
if (buf1[i]!=buf2[i])
return false;
}
}

public static bool Compare2( Class1 c1, Class1 c2 )
{
Type t = c1.GetType();

foreach (PropertyInfo p in t.GetProperties())
{
if (p.GetValue(c1,null)!=p.GetValue(c2,null))
return false;
}
return true;
}
}
}

"bengamin" <sa***@langchao.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
Hi,Marcin

Thany you !

The example you give have a presupposition/precondition that we must know the properties ,but the fact is we don't know the properties.

Today i found a new way to implement the function
This is the example code:

Class1 a = new Class1();
Class1 b = new Class1();
XmlSerializer formatter = new XmlSerializer(typeof(Class1));
Byte[] buff = new Byte[1024];
Stream stream = new MemoryStream(buff);
formatter.Serialize(stream, a);
Byte[] buff1 = new Byte[1024];
Stream stream1 = new MemoryStream(buff1);
formatter.Serialize(stream1, b);

for(int i = 0 ; i < buff.Length ; i ++ )
{
if (buff[i] != buff1[i])
{
label1.Text = "false";
return;
}
}
label1.Text = "true";
But there is some problem :
1, HashTable can not be xmlserializer;
2, The sample class i used for example is ok ,but when turn to the class

of
our business layer,
there come an error when step into "Stream stream = new
MemoryStream(buff);"
I don't know why ,is somebody can give me some help?

Thank you!

"Marcin Grzêbski" <mg*******@taxussi.spam.com.stop.pl> ????
news:ce***********@mamut.aster.pl...
Hi bengamin,

Here is your sample:

public class Person
: IComparable
{
private int age;
private string lastName;
private string firstName;
private bool maleGender;

public int CompareTo(object obj) {
Person secondPerson=obj as Person;
if( secondPerson==null ) {
throw new ArgumentNullException("obj");
}
int result;
result=(age-obj.age);
if( result!=0 ) {
return result;
}
result=lastName.CompareTo(obj.lastName);
if( result!=0 ) {
return result;
}
result=firstName.CompareTo(obj.firstName);
if( result!=0 ) {
return result;
}
result=maleGender.CompareTo(obj.maleGender);
return result;
}

}

Cheers!

Marcin

> Thank you for your help!
> Can you give us some example?
>
> "Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> ????
> news:ce**********@atlantis.news.tpi.pl...
>
>>Hi,
>>
>>Good practise is to implement "IComparable" interface.
>>
>>int CompareTo(Object obj)
>>- it should return zero if "obj" is equal to base object.
>>
>>Then you have to (easily) override the "Equals()", as follow:
>>
>>public override bool Equals(Object obj) {
>> return (this.CompareTo(obj)==0);
>>}
>>
>>Cheers
>>
>>Marcin
>>
>>
>>>Hi,
>>>
>>>I have a C# class and two instance of the class;
>>>
>>>the class have some property.
>>>
>>>I want to compare the property value of the two instance
>>>
>>>How should i do? override == ? use delegate ?
>>>
>>>I am sorry ,my english was poor.
>>
>>
>
>



Nov 16 '05 #17
Hi,

After testing ,I found the byte comparison is not a reliable way
first, I get data from web page and store into a instance
second, I store the instance to a session ,
and then I get data from the web page again without any changeing ,
even the web page data didn't change, there will be something like
arraylist._version have changed and the compare function will return false ;

So, i think we should found another solution or try to finish the solution
of reflection

Go on please!

Thank you!

"richlm" <ri*****@h0tmai1.com> дÈëÓʼþ
news:uc**************@TK2MSFTNGP11.phx.gbl...
If you really want to do byte comparison, you would be better off using
BinaryFormatter.
(Note your XmlSerializer only takes the public properties). See Compare1
below.

Another good solution where you don't know the properties is to use
reflection. See Compare2 below - much simpler.

(sample code for illustration - not tested...)
*************

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;

namespace ClassLibrary1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
int i = 0;
string s = "";
object o = null;

public Class1()
{
}

public static bool Compare1( Class1 c1, Class1 c2 )
{
BinaryFormatter f1 = new BinaryFormatter();
MemoryStream s1 = new MemoryStream();
BinaryFormatter f2 = new BinaryFormatter();
MemoryStream s2 = new MemoryStream();

f1.Serialize(s1,c1);
f2.Serialize(s2,c2);

byte[] buf1 = s1.ToArray();
byte[] buf2 = s2.ToArray();

if (buf1.Length==buf2.Length)
return false;

for (int i=0; i<buf1.Length; i++)
{
if (buf1[i]!=buf2[i])
return false;
}
}

public static bool Compare2( Class1 c1, Class1 c2 )
{
Type t = c1.GetType();

foreach (PropertyInfo p in t.GetProperties())
{
if (p.GetValue(c1,null)!=p.GetValue(c2,null))
return false;
}
return true;
}
}
}

"bengamin" <sa***@langchao.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
Hi,Marcin

Thany you !

The example you give have a presupposition/precondition that we must know the properties ,but the fact is we don't know the properties.

Today i found a new way to implement the function
This is the example code:

Class1 a = new Class1();
Class1 b = new Class1();
XmlSerializer formatter = new XmlSerializer(typeof(Class1));
Byte[] buff = new Byte[1024];
Stream stream = new MemoryStream(buff);
formatter.Serialize(stream, a);
Byte[] buff1 = new Byte[1024];
Stream stream1 = new MemoryStream(buff1);
formatter.Serialize(stream1, b);

for(int i = 0 ; i < buff.Length ; i ++ )
{
if (buff[i] != buff1[i])
{
label1.Text = "false";
return;
}
}
label1.Text = "true";
But there is some problem :
1, HashTable can not be xmlserializer;
2, The sample class i used for example is ok ,but when turn to the class

of
our business layer,
there come an error when step into "Stream stream = new
MemoryStream(buff);"
I don't know why ,is somebody can give me some help?

Thank you!

"Marcin Grzêbski" <mg*******@taxussi.spam.com.stop.pl> ????
news:ce***********@mamut.aster.pl...
Hi bengamin,

Here is your sample:

public class Person
: IComparable
{
private int age;
private string lastName;
private string firstName;
private bool maleGender;

public int CompareTo(object obj) {
Person secondPerson=obj as Person;
if( secondPerson==null ) {
throw new ArgumentNullException("obj");
}
int result;
result=(age-obj.age);
if( result!=0 ) {
return result;
}
result=lastName.CompareTo(obj.lastName);
if( result!=0 ) {
return result;
}
result=firstName.CompareTo(obj.firstName);
if( result!=0 ) {
return result;
}
result=maleGender.CompareTo(obj.maleGender);
return result;
}

}

Cheers!

Marcin

> Thank you for your help!
> Can you give us some example?
>
> "Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> ????
> news:ce**********@atlantis.news.tpi.pl...
>
>>Hi,
>>
>>Good practise is to implement "IComparable" interface.
>>
>>int CompareTo(Object obj)
>>- it should return zero if "obj" is equal to base object.
>>
>>Then you have to (easily) override the "Equals()", as follow:
>>
>>public override bool Equals(Object obj) {
>> return (this.CompareTo(obj)==0);
>>}
>>
>>Cheers
>>
>>Marcin
>>
>>
>>>Hi,
>>>
>>>I have a C# class and two instance of the class;
>>>
>>>the class have some property.
>>>
>>>I want to compare the property value of the two instance
>>>
>>>How should i do? override == ? use delegate ?
>>>
>>>I am sorry ,my english was poor.
>>
>>
>
>



Nov 16 '05 #18

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

Similar topics

7
by: Sean J. Fraley | last post by:
This code illustrates what I'm confused about: template<typename T> class foo { public: template<typename U> void fooFunction(const foo<U>& x) {
2
by: Locia | last post by:
How can I compare "if argument"? example: if (leftExpression==RightExpression) After parsing I know the type of RightExpression. I suppone that if RightExpression is wrap into " " is a...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
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...
9
by: Martoon | last post by:
I want to instantiate an STL map with my own compare function, and I want to pass a parameter to the compare function that will be stored and used for all comparisons in that map instance. As an...
10
by: NewToCPP | last post by:
I am having problem with key compare in stl map. Below is part of my code .. could anyone tell me what might be wrong here... I am using VC++ 6.0 code: ===== class MyKey {
2
by: Avi | last post by:
Hi all, I have two instances of the same class. Is it possible to compare private members of two classes and to come with a list of differences? Thanks, Avi
5
by: S S | last post by:
Hi I have a requirement where I am declaring a map within a class. class abc { map <void*, void*mMap; // I do not pass compare struct here. .... }; Here I am not passing compare function,...
21
by: Peter Duniho | last post by:
On Fri, 18 Jul 2008 07:03:37 -0700, Ben Voigt <rbv@nospam.nospamwrote: I agree whole-heartedly about being closer to Java. But the OP didn't ask about Java. :) I disagree on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...

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.