473,218 Members | 1,436 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,218 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 1417
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Shabam | last post by:
A web application of mine developed using C# + MS SQL runs fine normally. However when I stress test it with a load testing software (using about 60 simultaneous users) some instances start...
3
by: matt p | last post by:
example: FunClass myfun; FunClass *lotsofunptr=&myfun; myfun; //calls the overloaded operator; lotsofunptr->;//error help is much apreciated
8
by: SpOiLeR | last post by:
Hello! I have a matrix class like this: class MyObject; // MyMatrix is contains MyObjects class MyMatrix { public: ...
4
by: EL OSO | last post by:
Hi, let's suppose I have a class A { int x; ... public static bool operator == (A a1 , A a2) { return (a1.x==a2.x); }
7
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will...
5
by: shaun roe | last post by:
I am about to replace a plain function which returns a string with an object; I am trying to preserve the interface to clients of the function, while imbuing the function with some internal state....
13
by: Karthik | last post by:
In a recent techincal interview on C++, I came across this strange question.... How to ensure that your class object is always created in heap (by a new operator)? The compiler should throw...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
1
by: subramanian100in | last post by:
Consider the following: int x; int y; int z; (x+y) = z; For this statement, I get the following error with g++ compiler: error: non-lvalue in assignment Suppose I have a class Test and x,...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.