473,324 Members | 2,246 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,324 software developers and data experts.

operator== and null

Hi there,
Just wondering what is the correct way of implementing the comparison
operator in c#
operator==

My problem is when one or more of the objects is null,
I've cast to object to try compare them for null, trying to compare on
AlarmProfile objects will recrusivly call my comparison operator so this is
not an option.

code......
public static bool operator==(AlarmProfile lhs, AlarmProfile rhs)
{
object objLeft = lhs as object;
object objRight = rhs as object;
if (objLeft == null)
{
if (objRight == null)
return true;
else
return false;
}

return lhs.Equals(rhs);
}

public override bool Equals(object obj)
{
AlarmProfile rhs = (AlarmProfile)obj;
if (rhs == null)
return false;

return this.ProfileId == rhs.ProfileId;
}
So I'm cheking for null all over the place, it this correct design?

thanks in advance
Brian.
Nov 16 '05 #1
5 1974
You should do it like this: call equals method from comparison operator not
the other way around:

public override bool Equals(object obj)
{
return obj!=null && ((ZephirDate)obj).Date==this.Date;
}

public static bool operator==(ZephirDate d1, ZephirDate d2)
{
if (((object)d1)!=null)
return d1.Equals(d2);
else if (((object)d2)!=null)
return d2.Equals(d1);
else
return true;
}
Nov 16 '05 #2
thanks for your help.

"cody" wrote:
You should do it like this: call equals method from comparison operator not
the other way around:

public override bool Equals(object obj)
{
return obj!=null && ((ZephirDate)obj).Date==this.Date;
}

public static bool operator==(ZephirDate d1, ZephirDate d2)
{
if (((object)d1)!=null)
return d1.Equals(d2);
else if (((object)d2)!=null)
return d2.Equals(d1);
else
return true;
}

Nov 16 '05 #3
cody wrote:
You should do it like this: call equals method from comparison operator not
the other way around:
My usual idiom:

public override bool Equals(Object obj) { return Equals(obj as Foo); }
public virtual bool Equals(Foo other) {
// leave "==this" test out if property comparisons are cheap
if ( other == this )
return true;
else if ( other == null )
return false;
else if ( other.property != this.property )
return false;
// any number of property tests go here...
else
return true;
}
public static bool Operator==(Foo foo1, Foo foo2) {
if ( ((object)foo1) == null )
return ((object)foo2) == null;
else
return foo1.Equals(foo2);
}
if (((object)d1)!=null)
return d1.Equals(d2);
else if (((object)d2)!=null)
return d2.Equals(d1);


this.Equals(Object o) should never return true if o == null, should it?

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 16 '05 #4
> this.Equals(Object o) should never return true if o == null, should it?

Sure it shouldn't. Why do you ask? Thats why I have
"return obj!=null && ((ZephirDate)obj).Date==this.Date;"
in my equals method.
Nov 16 '05 #5
cody wrote:
this.Equals(Object o) should never return true if o == null, should it?
Sure it shouldn't. Why do you ask? Thats why I have
"return obj!=null && ((ZephirDate)obj).Date==this.Date;"
in my equals method.
It's not that it's a big deal,... I ask Because of the code i quoted
above the question, in the "public static bool Operator==":
if (((object)d1)!=null)
return d1.Equals(d2);
else if (((object)d2)!=null)
return d2.Equals(d1);

[... cutted rest ...]

Which actually invokes d2.Equals(null). It runs if, and only if, d1 ==
null && d2 != null. I usually write:

if ( ((object)foo1) == null )
return ((object)foo2) == null;
else
return foo1.Equals(foo2);

instead.

Your code uses right-hand-evaluation of ==, if and only if left-hand
side is null, that's what was a bit wierd to me. My code does left-hand
evaluation always, which is clearer (to me, at least :), and can be done
if "this.Equals(null) != true" is guaranteed.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 16 '05 #6

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

Similar topics

24
by: Marcin Vorbrodt | last post by:
Is there any reason why auto_ptr does not have the cast operator like this: operator void* (); So that one could easily test auto_ptr for NULL ??? Standard does not declare such operator. Is...
3
by: ganesh.tambat | last post by:
Hi, Please see below a piece of code. Here I am trying to create a linked list by attaching two linked list together. I have overloaded operator + for this. Now the output always says that the...
80
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one...
10
by: Wilhelm Heramb | last post by:
What is the best practice to implement operator overloading for == and != that handles null on either lhs or rhs. Andreas :-)
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
22
by: Luke Matuszewski | last post by:
We all know that feature detection technique works from very beggining of user-agents supporting JavaScript. We can alway check if eg. document has a write property or smth else: ...
12
by: mathboy2 | last post by:
Can anyone tell me what I'm doing wrong in the below snippet of code? I'm simply trying to override the == operator but having no luck. I can't even get it to hit a break point at the top of the ==...
0
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.