473,320 Members | 2,097 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,320 software developers and data experts.

Problems overrideing the == operator

Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
LoggerUtil.LogError(MethodBase.GetCurrentMethod(),
ex.Message, ex.StackTrace);
}

return ret;
}

public static bool operator !=(MyState state1, MyState state2)
{
return !(state1 == state2);
}

public override bool Equals(object obj)
{
bool ret = false;
if (obj != null && obj is MyState)
{
ret = this.PrimitiveState ==
((MyState)obj).PrimitiveState;
}
return ret;
}

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

Jan 22 '07 #1
12 1180
Hi,

have you tried

public static bool operator ==(MyState state1, MyState state2)
{
return a.Equals(b);
}

that should work.

HTH,
James.

mathboy2 wrote:
Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
LoggerUtil.LogError(MethodBase.GetCurrentMethod(),
ex.Message, ex.StackTrace);
}

return ret;
}

public static bool operator !=(MyState state1, MyState state2)
{
return !(state1 == state2);
}

public override bool Equals(object obj)
{
bool ret = false;
if (obj != null && obj is MyState)
{
ret = this.PrimitiveState ==
((MyState)obj).PrimitiveState;
}
return ret;
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}
Jan 22 '07 #2
It's a good idea, but the problem is that I can't even get the thread
to enter the public static bool operator ==() method in the first
place. The logic is the same so once I can get it in there it should
just work...

Dan

pigeonrandle wrote:
Hi,

have you tried

public static bool operator ==(MyState state1, MyState state2)
{
return a.Equals(b);
}

that should work.

HTH,
James.

mathboy2 wrote:
Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
LoggerUtil.LogError(MethodBase.GetCurrentMethod(),
ex.Message, ex.StackTrace);
}

return ret;
}

public static bool operator !=(MyState state1, MyState state2)
{
return !(state1 == state2);
}

public override bool Equals(object obj)
{
bool ret = false;
if (obj != null && obj is MyState)
{
ret = this.PrimitiveState ==
((MyState)obj).PrimitiveState;
}
return ret;
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}
Jan 22 '07 #3

mathboy2 wrote:
Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)
Um, what do you think is going to happen here? state1 != null will call
operator!= which
dereferences null and... splat.

Try this:

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;
try
{
if (!state1.Equals(null)&& !state2.Equals(null) )
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}
return ret;
}

Remember, while in Equals the object is of TYPE object, in the != and
== operators
the object is of TYPE MyState, recursing itself to death.

Matt

Jan 22 '07 #4
Have you tried taking the 'static' out? It might be something to do
with c# defaulting to object==object since the other members of your
class are not static.
I can't say i've tried this in c#, but if i say that it's impossible(!)
then someone is sure to correct me...

Good luck,
James.

mathboy2 wrote:
It's a good idea, but the problem is that I can't even get the thread
to enter the public static bool operator ==() method in the first
place. The logic is the same so once I can get it in there it should
just work...

Dan

pigeonrandle wrote:
Hi,

have you tried

public static bool operator ==(MyState state1, MyState state2)
{
return a.Equals(b);
}

that should work.

HTH,
James.

mathboy2 wrote:
Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:
>
MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}
>
Note that the following works fine for me:
>
if (a.Equals(b))
{
...
}
>
I don't know if I'm using it incorrectly because the == must be static
or what...
>
Thanks,
Dan
>
>
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;
>
public LINE_STATE PrimitiveState
{
get { return m_state; }
}
>
public MyState(LINE_STATE state)
{
m_state = state;
}
>
public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;
>
try
{
if (state1 != null && state2 != null)
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
LoggerUtil.LogError(MethodBase.GetCurrentMethod(),
ex.Message, ex.StackTrace);
}
>
return ret;
}
>
public static bool operator !=(MyState state1, MyState state2)
{
return !(state1 == state2);
}
>
public override bool Equals(object obj)
{
bool ret = false;
if (obj != null && obj is MyState)
{
ret = this.PrimitiveState ==
((MyState)obj).PrimitiveState;
}
return ret;
}
>
public override int GetHashCode()
{
return base.GetHashCode();
}
>
>
}
Jan 22 '07 #5
Scratch that comment about taking the static modifer out :0).
pigeonrandle wrote:
Have you tried taking the 'static' out? It might be something to do
with c# defaulting to object==object since the other members of your
class are not static.
I can't say i've tried this in c#, but if i say that it's impossible(!)
then someone is sure to correct me...

Good luck,
James.

mathboy2 wrote:
It's a good idea, but the problem is that I can't even get the thread
to enter the public static bool operator ==() method in the first
place. The logic is the same so once I can get it in there it should
just work...

Dan

pigeonrandle wrote:
Hi,
>
have you tried
>
public static bool operator ==(MyState state1, MyState state2)
{
return a.Equals(b);
}
>
that should work.
>
HTH,
James.
>
mathboy2 wrote:
Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan


public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
LoggerUtil.LogError(MethodBase.GetCurrentMethod(),
ex.Message, ex.StackTrace);
}

return ret;
}

public static bool operator !=(MyState state1, MyState state2)
{
return !(state1 == state2);
}

public override bool Equals(object obj)
{
bool ret = false;
if (obj != null && obj is MyState)
{
ret = this.PrimitiveState ==
((MyState)obj).PrimitiveState;
}
return ret;
}

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


}
Jan 22 '07 #6
Matt,

A very valid point. This is something obviously wrong with my logic and
I'll have to deal with that... but it doesn't solve the main
problem....

I can change my code to what is below and I still don't hit a break
point at the top of operator == overload.

Thanks,
Dan
public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;
try
{
ret = state1.PrimitiveState == state2.PrimitiveState;
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}
return ret;
}

Matt wrote:
mathboy2 wrote:
Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)

Um, what do you think is going to happen here? state1 != null will call
operator!= which
dereferences null and... splat.

Try this:

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;
try
{
if (!state1.Equals(null)&& !state2.Equals(null) )
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}
return ret;
}

Remember, while in Equals the object is of TYPE object, in the != and
== operators
the object is of TYPE MyState, recursing itself to death.

Matt
Jan 22 '07 #7
sorry,

here is the code:

public static bool operator ==(MyState state1, MyState state2)

{
bool ret = false;

try
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}

return ret;
}

Jan 22 '07 #8

mathboy2 wrote:
Matt,

A very valid point. This is something obviously wrong with my logic and
I'll have to deal with that... but it doesn't solve the main
problem....

I can change my code to what is below and I still don't hit a break
point at the top of operator == overload.
Rather odd. I copied your code into a test program, and implemented
LINE_STATE as a simple enum. My test crashed the first time, due to
the issue I mentioned. When I changed it, it ran fine and the objects
compared fine.

Could it be that you are simply running into a weird debugger issue,
rather than a code problem? Try just pulling the class into a separate
test program and see if you see what I do.

Matt

Jan 22 '07 #9
Matt,

You're right! It worked perfectly in a new test app. Hmmm... well
obviously something is very different in my large environment, but at
least I can track it down now that I know that I'm not doing something
wrong with the == overload!

Thanks a bunch for you help!

Dan

Matt wrote:
mathboy2 wrote:
Matt,

A very valid point. This is something obviously wrong with my logic and
I'll have to deal with that... but it doesn't solve the main
problem....

I can change my code to what is below and I still don't hit a break
point at the top of operator == overload.

Rather odd. I copied your code into a test program, and implemented
LINE_STATE as a simple enum. My test crashed the first time, due to
the issue I mentioned. When I changed it, it ran fine and the objects
compared fine.

Could it be that you are simply running into a weird debugger issue,
rather than a code problem? Try just pulling the class into a separate
test program and see if you see what I do.

Matt
Jan 22 '07 #10

"Matt" <ma********@sprynet.comwrote in message
news:11*********************@51g2000cwl.googlegrou ps.com...
>
mathboy2 wrote:
>Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)

Um, what do you think is going to happen here? state1 != null will call
operator!= which
dereferences null and... splat.
His operator== tries to be careful not to dereference null. However, the
infinite recursion really is a problem. That's trivial to fix, however,
since the current operator== defines null as not equal to anything, even
null, the if condition will always succeeds, null is dereferenced after all,
and splat.
>
Try this:

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;
try
{
if (!state1.Equals(null)&& !state2.Equals(null) )
Rather object.ReferenceEquals(state1, null) or just null == (object)state1
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}
return ret;
}

Remember, while in Equals the object is of TYPE object, in the != and
== operators
the object is of TYPE MyState, recursing itself to death.

Matt

Jan 22 '07 #11
Hi Dan,

Just FYI:

You should think about making your class a struct since you're giving it
value-type semantics.

If I needed to use an instance of your class in my code, I would assume
reference-equality, which would make my life difficult if I wanted to use it
in a Dictionary since two completely different instances might return the
same value, unbeknownst to me.

--
Dave Sexton
http://davesexton.com/blog

"mathboy2" <da******@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
LoggerUtil.LogError(MethodBase.GetCurrentMethod(),
ex.Message, ex.StackTrace);
}

return ret;
}

public static bool operator !=(MyState state1, MyState state2)
{
return !(state1 == state2);
}

public override bool Equals(object obj)
{
bool ret = false;
if (obj != null && obj is MyState)
{
ret = this.PrimitiveState ==
((MyState)obj).PrimitiveState;
}
return ret;
}

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

Jan 23 '07 #12
Hi,

for preventing recursion you could cast one of the operands to object:
instead of

if (state1 != null && state2 != null)

you also could use:

if ((objecj)state1!=null && (object)state2!=null)

maybe more readable than:

if (!state1.Equals(null)&& !state2.Equals(null) )
"Matt" <ma********@sprynet.comschrieb im Newsbeitrag
news:11*********************@51g2000cwl.googlegrou ps.com...
>
mathboy2 wrote:
>Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan
public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)

Um, what do you think is going to happen here? state1 != null will call
operator!= which
dereferences null and... splat.

Try this:

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;
try
{
if (!state1.Equals(null)&& !state2.Equals(null) )
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}
return ret;
}

Remember, while in Equals the object is of TYPE object, in the != and
== operators
the object is of TYPE MyState, recursing itself to death.

Matt

Jan 23 '07 #13

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

Similar topics

9
by: mathias | last post by:
I would like to define a custom operator in Python. (It is not about overloading an existing operator but about defining special new operators) Is this possible without deeply manipulating the...
2
by: -Steve- | last post by:
Okay I have a bunch of code below. Hope it comes across readable. The problem I'm having is that in the lines under main(): cout << a << endl; Is going into the code for IntArray(const...
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
2
by: psane | last post by:
I have the following code #include <iostream> #include <string> using namespace std; class superclass { public: superclass () {}
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
34
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
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: Jim Michaels | last post by:
friend fraction& operator+=(const fraction& rhs); fraction.h(64) Error: error: 'fraction& operator+=(const fraction&)' must take exactly two arguments I practically pulled this out of a C++...
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.