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

Overloading operator== and comparing to null

Not sure how I've not come across this before...

I've overloaded operator == for one of my classes using
the following code.

public static bool operator ==(Stock x, Stock y)
{
return x.Equals(y);
}
public override bool Equals(object obj)
{
Stock queryObject = null;
if (obj is Stock)
queryObject = (Stock) obj;
else
return false;

return ((queryObject.StockCode == this.StockCode));
}

In another class I have the following code:

private Stock _stock = null;
private Stock getStock()
{
if (_stock == null) // Error here...
{
_stock = new Stock()
// populate details
}
}

My error occurs, because I initialise my local copy to
null, so it has no methods...
How do I check whether or not my object is null??

Thanks in advance
Paul
Nov 15 '05 #1
4 14473
"Paul Lyons" <pa**@the-lyons.removethis.com> wrote in message
news:0e****************************@phx.gbl...
public static bool operator ==(Stock x, Stock y)
{
return x.Equals(y);
}


you need to check if x is not null

if (x == null)
return y == null;
else
return x.Equals(y);
Nov 15 '05 #2
On Wed, 30 Jul 2003 15:03:35 -0400, "Jack Hanebach"
<ha*********@spam.suscom.net> wrote:
you need to check if x is not null

if (x == null)
return y == null;
else
return x.Equals(y);


Watch out for a nasty trap here. Your code will result in an infinite
recursion because (x == null) invokes the strongly typed overload of
operator== which is currently executing.

You have to use a System.Object comparison instead, such as:

if ((object) x == null)
return ((object) y == null);
else
return x.Equals(y);
--
http://www.kynosarges.de
Nov 15 '05 #3
"Christoph Nahr" <ch************@kynosarges.de> wrote in message
news:dt********************************@4ax.com...
if (x == null)
return y == null;
else
return x.Equals(y);


Watch out for a nasty trap here. Your code will result in an infinite
recursion because (x == null) invokes the strongly typed overload of
operator== which is currently executing.

You have to use a System.Object comparison instead, such as:

if ((object) x == null)
return ((object) y == null);
else
return x.Equals(y);


Good catch! I guess if (null == x) would work too...
Nov 15 '05 #4
Jack Hanebach <ha*********@spam.suscom.net> wrote:
Good catch! I guess if (null == x) would work too...


No, that would still call the overloaded == operator. See section
14.2.4 of the C# specification - the order isn't taken into account
when selecting the operator implementation to use.

I'd also argue that even if it *did* work, it would be a bad way of
doing it, as anyone just looking at it might not realise the purpose of
specifying the operator that particular way, and change it to the more
natural (in English) if (x==null). Fortunately, that's not an issue
anyway :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
4
by: Mohammad | last post by:
I'm implementing a c++ template class CScan to minipulate a series of numbers. I have implemented operator() to select a range of numbers it works fine for an expression like: scan1 = scan2(0,...
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...
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: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
3
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
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...
1
by: xkenneth | last post by:
Hi, I'm writing a sparse matrix class for class and I cannot seem to get operator overloading to work properly. I've overloaded an operator with the code here. matrix operator +(matrix one,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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

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.