Testing for instance of object when operator overloaded

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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) );
}
 
Jesper said:
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
 
Thanks Jon!.
Regards Jesper, DK.

Jon Skeet said:
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
 
Jesper said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top