471,354 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Testing for instance of object when operator overloaded

Hi,

I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an object.

ID id = new ID(1,2)
.....
e.g. if ( id != null ) <- this line calls the static overload, I guees,
instead of testing whether the id is set to an object or not. How do I test
for an instance on id, do I have to test in another way, or are my overloads
written in a wrong way?

thanks.
public static bool operator==( ID a, ID b )
{
return ( a.no == b.no && a.rev == b.rev );
}

public static bool operator!=( ID a, ID b )
{
return ( !(a.no == b.no && a.rev == b.rev) );
}
Jan 11 '06 #1
3 1349
Jesper wrote:
I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an object.

ID id = new ID(1,2)
....
e.g. if ( id != null ) <- this line calls the static overload, I guees,
instead of testing whether the id is set to an object or not. How do I test
for an instance on id, do I have to test in another way, or are my overloads
written in a wrong way?


You need to check for a and b being null in your overloaded operators.
In order to perform that check, you'll need to use
Object.ReferenceEquals or
cast the parameters to object before testing against null, otherwise
you'll just recurse.

Let me know if this isn't enough detail to get you going.

(You might want to implement != by just return !(a==b) by the way, to
keep all your common logic in one place.)

Jon

Jan 11 '06 #2
Thanks Jon!.
Regards Jesper, DK.

"Jon Skeet [C# MVP]" wrote:
Jesper wrote:
I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an object.

ID id = new ID(1,2)
....
e.g. if ( id != null ) <- this line calls the static overload, I guees,
instead of testing whether the id is set to an object or not. How do I test
for an instance on id, do I have to test in another way, or are my overloads
written in a wrong way?


You need to check for a and b being null in your overloaded operators.
In order to perform that check, you'll need to use
Object.ReferenceEquals or
cast the parameters to object before testing against null, otherwise
you'll just recurse.

Let me know if this isn't enough detail to get you going.

(You might want to implement != by just return !(a==b) by the way, to
keep all your common logic in one place.)

Jon

Jan 11 '06 #3
Jesper wrote:
Hi,

I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an object.

ID id = new ID(1,2)
....
e.g. if ( id != null ) <- this line calls the static overload, I guees,
instead of testing whether the id is set to an object or not. How do I test
for an instance on id, do I have to test in another way, or are my overloads
written in a wrong way?

thanks.
public static bool operator==( ID a, ID b )
{
return ( a.no == b.no && a.rev == b.rev );
}

public static bool operator!=( ID a, ID b )
{
return ( !(a.no == b.no && a.rev == b.rev) );
}


Here is the "template" I follow when I oveload op==:

class ID
{
public override bool Equals(object obj)
{
ID rhs = obj as ID;
if(rhs == null)
return false;
return this.no == rhs.no && this.rev == rhs.rev;
}
public override int GetHashCode()
{
return no.GetHashCode() ^ rev.GetHashCode();
}
public static bool operator ==(ID lhs, ID rhs)
{
return object.Equals(lhs, rhs);
}
public static bool operator !=(ID lhs, ID rhs)
{
return !object.Equals(lhs, rhs);
}
private int no, rev;
}

In this way i have all the logic in the Equals() method which is getting
called from the operators.

HTH,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Jan 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Shabam | last post: by
4 posts views Thread by EL OSO | last post: by
7 posts views Thread by Genival Carvalho | last post: by
13 posts views Thread by Karthik | last post: by
1 post views Thread by subramanian100in | last post: by

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.