473,228 Members | 1,770 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,228 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 2347
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: 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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.