473,414 Members | 1,750 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,414 software developers and data experts.

Operator== overload problem

I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"

Does anyone have any idea how to correct this problem?

Example source follows:

Thanks,
Tony

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1, TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1, TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this line
oTestObject = new TestObject();
}
}
Jan 26 '06 #1
9 2357
Yes. Don't overload == for anything except value types.

I don't understand why you would return other than a bool? If you want a
method to return an instance of a class based upon the comparison, then write
a method that calls the comparison and returns the class instance based on
the result but don't overload the comparison.

When you do have to overload equivelence in objects (and I recommend doing
it more often than not) overload Object.Equals(), but you still have to
return a bool. You can't change the return type in an overload. You could,
but I wouldn't recommend it, use the keyword "new" instead of "override" to
hide the original operator or method and then you could return a different
type.

Hiding base class members should be done with caution and great reserve.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Tony" wrote:
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"

Does anyone have any idea how to correct this problem?

Example source follows:

Thanks,
Tony

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1, TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1, TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this line
oTestObject = new TestObject();
}
}

Jan 26 '06 #2
I just re-read my post... Replace overload with override in all instances :)

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Dale" wrote:
Yes. Don't overload == for anything except value types.

I don't understand why you would return other than a bool? If you want a
method to return an instance of a class based upon the comparison, then write
a method that calls the comparison and returns the class instance based on
the result but don't overload the comparison.

When you do have to overload equivelence in objects (and I recommend doing
it more often than not) overload Object.Equals(), but you still have to
return a bool. You can't change the return type in an overload. You could,
but I wouldn't recommend it, use the keyword "new" instead of "override" to
hide the original operator or method and then you could return a different
type.

Hiding base class members should be done with caution and great reserve.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Tony" wrote:
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"

Does anyone have any idea how to correct this problem?

Example source follows:

Thanks,
Tony

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1, TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1, TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this line
oTestObject = new TestObject();
}
}

Jan 26 '06 #3
I'm doing this because the classes I'm building allow me to generate SQL
using the C# syntax.
The details get a bit more technical, but I will give it a try.

Select.Where( Table1["IdColumn"] == Table2["IdColumn"] & Table1["Name"] ==
Table2["Name"]);

The above C# code generates the following SQL:
"Where Table1.IdColumn = Table2.IdColumn and Table1.Name = Table2.Name"

Table1["IdColumn"] == Table2["IdColumn"] generates a Condition object
returned from the Columns operator==:
Where( ) accepts a Condition.
You can create a new Condition by: Condition & Condition and so forth.

The problem is when I need to check the value of my Column class (returned
from a Table) to see if it's null or for any other reason.

Column oColumn = oTable1["IdColumn"];

if (oColumn == null) // generates the ambiguous error

however, I have found a work around by casting it to object first to do a
reference compare but it's less that intuitive when you see it:

if ((object)oColumn == null) //. compiles fine

Tony

"Dale" <da******@nospam.nospam> wrote in message
news:4D**********************************@microsof t.com...
Yes. Don't overload == for anything except value types.

I don't understand why you would return other than a bool? If you want a
method to return an instance of a class based upon the comparison, then
write
a method that calls the comparison and returns the class instance based on
the result but don't overload the comparison.

When you do have to overload equivelence in objects (and I recommend doing
it more often than not) overload Object.Equals(), but you still have to
return a bool. You can't change the return type in an overload. You
could,
but I wouldn't recommend it, use the keyword "new" instead of "override"
to
hide the original operator or method and then you could return a different
type.

Hiding base class members should be done with caution and great reserve.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Tony" wrote:
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"

Does anyone have any idea how to correct this problem?

Example source follows:

Thanks,
Tony

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1,
TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1,
TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this line
oTestObject = new TestObject();
}
}

Jan 26 '06 #4
The problem has nothing to do with returning something other than bool, the
same happens in that case also:

internal class Condition
{

}

internal class TestObject
{

public static bool operator==(TestObject oTestObject, string sValue)
{
return true; // a stub for illistration purposes
}

public static bool operator !=(TestObject oTestObject, string sValue)
{
return true; // a stub for illistration purposes
}

public static bool operator ==(TestObject oTestObject1, TestObject
oTestObject2)
{
return true; // a stub for illistration purposes
}

public static bool operator !=(TestObject oTestObject1, TestObject
oTestObject2)
{
return true; // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}
internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compiler error
oTestObject = new TestObject();
}
}

"Dale" <da******@nospam.nospam> wrote in message
news:4D**********************************@microsof t.com...
Yes. Don't overload == for anything except value types.

I don't understand why you would return other than a bool? If you want a
method to return an instance of a class based upon the comparison, then
write
a method that calls the comparison and returns the class instance based on
the result but don't overload the comparison.

When you do have to overload equivelence in objects (and I recommend doing
it more often than not) overload Object.Equals(), but you still have to
return a bool. You can't change the return type in an overload. You
could,
but I wouldn't recommend it, use the keyword "new" instead of "override"
to
hide the original operator or method and then you could return a different
type.

Hiding base class members should be done with caution and great reserve.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Tony" wrote:
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"

Does anyone have any idea how to correct this problem?

Example source follows:

Thanks,
Tony

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1,
TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1,
TestObject
oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this line
oTestObject = new TestObject();
}
}

Jan 26 '06 #5
Tony,

In your case, you want to write the code like this:

if (oTestObject == (TestObject) null) // compile error occurs on this line
oTestObject = new TestObject();

The reason you get the ambiguity error is because null doesn't have a
type.

What you might want to do is have a static instance of your test object
which represents null and replace null with that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tony" <to*****@spacecommand.net> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"

Does anyone have any idea how to correct this problem?

Example source follows:

Thanks,
Tony

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1,
TestObject oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1,
TestObject oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this line
oTestObject = new TestObject();
}
}

Jan 26 '06 #6
Thanks, but that doesn't work if the operator== returns an object and not
bool.

This only thing I have come up with that works is to cast it to (object)
first to do a reference compare, but it's not very intuitive.

if ((object)oTestObject == null)
oTestObject = new TestObject();

Tny
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eK**************@TK2MSFTNGP09.phx.gbl...
Tony,

In your case, you want to write the code like this:

if (oTestObject == (TestObject) null) // compile error occurs on this
line
oTestObject = new TestObject();

The reason you get the ambiguity error is because null doesn't have a
type.

What you might want to do is have a static instance of your test object
which represents null and replace null with that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tony" <to*****@spacecommand.net> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"

Does anyone have any idea how to correct this problem?

Example source follows:

Thanks,
Tony

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1,
TestObject oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1,
TestObject oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this line
oTestObject = new TestObject();
}
}


Jan 26 '06 #7
"Tony" <to*****@spacecommand.net> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"

Does anyone have any idea how to correct this problem?

Example source follows:

Thanks,
Tony

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject, string
sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1,
TestObject oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1,
TestObject oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this line
oTestObject = new TestObject();
}
}


There are 2 problems.

The error is because the compiler cannot tell whether null is supposed to be
a TestObject or a string.
[ essentially the call is TestObject.operator==(oTestObject,null) ]

The major error is that Equals() and ==() can return different values which
is grossly counter intuitive.

The "standard" interpretation of Equals() is that 2 things are NEVER equal
if they are of different types (except possibly stuff link equivalent
numeric valus). A TestObject is not a kind of string so
oTestObject.Equals("hello")
should return false (or your Condition equivalent) and consistency then
demands that
oTestObject == "hello"
should also always return false which in turn makes it unnecessary.
Jan 26 '06 #8
Tony wrote:
I have an operator== overload that compares two items and returns a
new class as the result of the comparison (instead of the normal bool)

I then get an ambiguous operater compile error when I attempt to
check to see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"
this is logical, as it matches with both. I had the same prob, and
solved it as below.

What you should do is this:
instead of creating overloads for string AND TestObject, make 1
method, with type 'object'.

Internally, test for the type of object or null. This way you can
avoid the compilation error and IF you ever need an overload, you can
then also use in your code:

testobject==DBNull.Value

which represents testing for null (for example, don't know where you
use it for).

FB

internal class Condition
{
}

internal class TestObject
{
public static Condition operator==(TestObject oTestObject,
string sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject,
string sValue)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator==(TestObject oTestObject1,
TestObject oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public static Condition operator!=(TestObject oTestObject1,
TestObject oTestObject2)
{
return new Condition(); // a stub for illistration purposes
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object aObject)
{
return base.Equals(aObject);
}

}

internal class Test
{
void Testmethod()
{
TestObject oTestObject = null;

if (oTestObject == null) // compile error occurs on this
line oTestObject = new TestObject();
}
}


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jan 26 '06 #9
Tony wrote:
I'm doing this because the classes I'm building allow me to generate
SQL using the C# syntax.
The details get a bit more technical, but I will give it a try.

Select.Where( Table1["IdColumn"] == Table2["IdColumn"] &
Table1["Name"] == Table2["Name"]);

The above C# code generates the following SQL:
"Where Table1.IdColumn = Table2.IdColumn and Table1.Name =
Table2.Name"

Table1["IdColumn"] == Table2["IdColumn"] generates a Condition
object returned from the Columns operator==:
Where( ) accepts a Condition.
You can create a new Condition by: Condition & Condition and so forth.

The problem is when I need to check the value of my Column class
(returned from a Table) to see if it's null or for any other reason.

Column oColumn = oTable1["IdColumn"];

if (oColumn == null) // generates the ambiguous error

however, I have found a work around by casting it to object first to
do a reference compare but it's less that intuitive when you see it:

if ((object)oColumn == null) //. compiles fine


if you're using it for SQL predicate production, you should go for a
single method with 'object' and compare with DBNull.Value. I do that
too in my O/R mapper, so I can do:

Predicate filter = (CustomerFields.CompanyName == DBNull.Value);

which produces a [Customers].[CompanyName] IS NULL predicate in SQL.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jan 26 '06 #10

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

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
5
by: phiefer3 | last post by:
I'm currently a student, but this problem isn't directly related to what I have to do on an assignment. It's just a problem I've had with some supporting features. First of all, I'm using MSVS...
2
by: Markjan | last post by:
I have a problem with classes and structures in classes (C++) I have to overload operator . class Data { public: class Proxy { //for overload operator Data& _a; int...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.