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

Overriding == and testing to null

Hi, let's suppose I have a

class A
{
int x;
...
public static bool operator == (A a1 , A a2)
{
return (a1.x==a2.x);
}
}

But, outside the class, I have a problem when I try to test a probably not
initialized class.
For instance, think about this code:
public void myFunc()
{
A a1;
A a2;
.....
if (a1 == null)
{
//some null value handling
}
....
}

The problem is that testing a1==null creates recursive calls to the operator
== of class A.
How do I test if something is null without having to invoke the == operator
for that "something"?
Tnx in advance.
Nov 17 '05 #1
4 1251
Dumb of me, I mixed two questions.
What I was trying to say is that the invoked operator == for class A creates
a null reference when it tries to access a2.x when a2 is null (as I wrote on
MyFunc)

I tried in the definition of operator == something like
if (a1==null) or (a2==null)
return false
else
return (a1.x == (a2.x)

BUT, comparing a1 == null creates the recursive call to operator == in class
A

Do I make myself more clear this time? :p

"EL OSO" <no***@nospam.com> escribió en el mensaje
news:Oa**************@TK2MSFTNGP09.phx.gbl...
Hi, let's suppose I have a

class A
{
int x;
...
public static bool operator == (A a1 , A a2)
{
return (a1.x==a2.x);
}
}

But, outside the class, I have a problem when I try to test a probably not
initialized class.
For instance, think about this code:
public void myFunc()
{
A a1;
A a2;
....
if (a1 == null)
{
//some null value handling
}
...
}

The problem is that testing a1==null creates recursive calls to the operator == of class A.
How do I test if something is null without having to invoke the == operator for that "something"?
Tnx in advance.

Nov 17 '05 #2
BUT, comparing a1 == null creates the recursive call to operator == in class
A


Cast to object first to prevent that.

if ( (object)a1 == null )

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3


EL OSO wrote:
I tried in the definition of operator == something like
if (a1==null) or (a2==null)
return false
else
return (a1.x == (a2.x)
That's not really what you want, is it, if (object)a1 == null &&
(object)a2 == null ;)

I use the pattern:

public static bool operator==(A a1, A a2) {
if ( (object)a1 == null )
return (object)a2 == null;
else if ((object)a2 == null)
return false;
else
return a1.x == a2.x
}
BUT, comparing a1 == null creates the recursive call to operator == in class
A
Yes, you just overloaded operator==(A,A), so that gets called to compare
a1 and null.

The "trick" is to make a1 staticly typed as something that compares by
reference with operator==: object.
Do I make myself more clear this time? :p


mostly,... I'm not that good at being clear myself.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #4
You could use object.ReferenceEquals().

"EL OSO" <no***@nospam.com> wrote in message
news:Oa**************@TK2MSFTNGP09.phx.gbl...
Hi, let's suppose I have a

class A
{
int x;
...
public static bool operator == (A a1 , A a2)
{
return (a1.x==a2.x);
}
}

But, outside the class, I have a problem when I try to test a probably not
initialized class.
For instance, think about this code:
public void myFunc()
{
A a1;
A a2;
....
if (a1 == null)
{
//some null value handling
}
...
}

The problem is that testing a1==null creates recursive calls to the
operator
== of class A.
How do I test if something is null without having to invoke the ==
operator
for that "something"?
Tnx in advance.

Nov 17 '05 #5

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

Similar topics

7
by: Filips Benoit | last post by:
Hi, TBL_CONTACT_PERSON CNTP_ID (auto) CNTP_LAST_NAME (required = yes) CNTP_FUNCTION (required = no) CNTP_..... (all required = no) FRM_CONTACT_PERSON_ADD_NEW Property DATA ENTRY = YES
19
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like...
6
by: Lance | last post by:
Hi, How do you test for an object existing in C#? C# seems to differentiate between 'null' and non-existance. In other languages I could test as so: if(ex.InnerException.StackTrace != null)...
5
by: Brian | last post by:
Hello all.. Am working on an Air Hockey game... have an table loaded into a picture box. The borders of the table are slightly slanted. Am using hit testing lines with GDI+ to manipulate the...
1
by: Anders Borum | last post by:
Hello! I have a framework where all objects are uniquely identified by a GUID (Global Unique Identifier). The objects are used in conjunction with lots of hashtables and I was thinking, that...
8
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the...
4
by: Mufasa | last post by:
I'm going to create a server control in ASP.Net/C# that reacts to a couple of buttons. Thing is - I want to have the functions there for the buttons so if somebody doesn't program the command for...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
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
marktang
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.