473,387 Members | 1,859 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,387 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 14476
"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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.