How to compare values of two objects in C#

S

Sukh

Hi,
I have two object of same class but I want to check if there is any
value different b/w both object or not

Is there any way...
Obj1.Equals(obj2) is not working for this.

Its in C#
Thanks.
Sukh
 
S

Sukh

Karl Thanks for reply...if reference is same then both object will be
always same....
Sukh.
 
S

sloan

You can also make a "smart" comparer....
This method (below) allows you to use 1 Comparer per object, instead of many
comparers for 1 object.

This assumes you already have an Employee (class), with .Age and .Name
properties.



using System;
using System.Collections;


namespace MyCompany.MyApplication.Comparers
{


internal sealed class EmployeeComparer : IComparer
{
private EmployeeSortColumns m_sortValue = EmployeeSortColumns.None ;

public enum EmployeeSortColumns
{
None = 0 , Age = 1 , Name = 2
}

public EmployeeComparer(EmployeeSortColumns sortValue)
{
m_sortValue = sortValue;
}

public int Compare(object x,object y)
{
switch(m_sortValue)
{

case EmployeeSortColumns.None :
return 0;


case EmployeeSortColumns.Name :

return ((Employee)x).Name .CompareTo(((Employee )y).Name );
//break;
case EmployeeSortColumns.Age :


return ((Employee)x).Age.CompareTo(((Employee )y).Age );

default:
return ((Employee)x).Name .CompareTo(((Employee )y).Name );
// break;
}
}
}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top