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. 17 8243
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
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******** ******@tk2msftn gp13.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.
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*****@h0tmai 1.com> wrote in message
news:OS******** ******@TK2MSFTN GP10.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******** ******@tk2msftn gp13.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.
Hi,
Good practise is to implement "IComparabl e" interface.
int CompareTo(Objec t 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.
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.FirstNa me==person2.Fir stName
&& person1.LastNam e==person2.Last Name
&& person1.Age==pe rson2.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*****@h0tmai 1.com> wrote in message news:OS******** ******@TK2MSFTN GP10.phx.gbl...
Recommended practice is that operator "==" and method "Object.Equal s(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****** ********@tk2msf tngp13.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.
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)instanc e1 == (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******** ***********@new s-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
ah well, he wrote value and not values... :)
"Marcin Grzębski" <mg*******@taxu ssi.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.FirstNa me==person2.Fir stName && person1.LastNam e==person2.Last Name && person1.Age==pe rson2.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*****@h0tmai 1.com> wrote in message news:OS******** ******@TK2MSFTN GP10.phx.gbl...
Recommended practice is that operator "==" and method "Object.Equal s(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.aspRichard.
"bengamin" <sa***@langchao .com> wrote in message news:ea****** ********@tk2msf tngp13.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.
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****@quickne t.nl> Đ´ČëÓĘĽţ
news:ex******** ******@TK2MSFTN GP12.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*****@h0tmai 1.com> wrote in message news:OS******** ******@TK2MSFTN GP10.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******** ******@tk2msftn gp13.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.
Thank you for your help!
Can you give us some example?
"Marcin Grzębski" <mg*******@taxu ssi.no.com.spam .pl> ????
news:ce******** **@atlantis.new s.tpi.pl... Hi,
Good practise is to implement "IComparabl e" interface.
int CompareTo(Objec t 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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
{
|
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 String.
else I check if RightExpression is a int.
else In final case, I check that RightExpression is a name of object
|
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
uniqueidentifier, originally taken from objectGUID from active directory
domain)
|
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 compare these two objects so that it will return true if both
object's members have the same value...
it is good if u can give me a single function or simple code snippet..
Thank U
--
Peter...
|
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 example, let's say the map key is char* strings, and I want the
comparison based on the nth character in the string. When the map is
instantiated, I need to somehow specify n. So I might have something
like this:
class MyClass
| |
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
{
|
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
|
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, but I want to do it into the
|
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 VB/VB.NET point, but I guess that's a matter of opinion
so not entirely unexpected. In spite of some fundamental conceptual
differences between C++ and C#, I find moving back and forth between those
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |