Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,605 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 924 people online right now. Registration is fast and FREE... Join Now!




Overidding operator==

 
Reply to this topicStart new topic

Overidding operator==, child class override

redivider
post 14 Jul, 2008 - 01:22 PM
Post #1


New D.I.C Head

*
Joined: 28 May, 2008
Posts: 10

I have a parent class, and two child classes.

CODE
public abstract class IndustryClassification
{ public bool operator ==(IndustryClassification ic1, IndustryClassification ic2);
}
public class IndustryClassification1 :IndustryClassification
{}
public class IndustryClassification2 :IndustryClassification
{}

I want to override the == operator, but I can't mark the method virtual or abstract (get error "Modifier not allowed"). When I have the operator override defined in the base class, the base == operator is called because the object is saved in a base class type.
If i don't override it in the base class it still never calls the child == operator (I think it just compares the addresses, or some other default comparison?.)

CODE

var ic1 = new IndustryClassification1(x,y);
var ic2 = new IndustryClassification1(x,y);
IndustryClassification[] list;//with ic1 and ic2

....
if (list[0] == list[1])    
{
  areEqual = false;
}

How can i get this to call the child == operator? I shouldn't have to cast it before testing. Right?
User is offlineProfile CardPM

Go to the top of the page

SpiderSpartan
post 14 Jul, 2008 - 01:35 PM
Post #2


D.I.C Head

Group Icon
Joined: 6 Feb, 2008
Posts: 66



Thanked 3 times

Dream Kudos: 25
My Contributions


ignore me if I'm looking at this wrong, but you don't have anything actually overwriting the operator. Have you purposefully left that code out of the post?
User is offlineProfile CardPM

Go to the top of the page

redivider
post 14 Jul, 2008 - 01:42 PM
Post #3


New D.I.C Head

*
Joined: 28 May, 2008
Posts: 10

QUOTE(SpiderSpartan @ 14 Jul, 2008 - 02:35 PM) *

Have you purposefully left that code out of the post?

Correct. The objects are fairly complex, so for brevity i left out the comparisons, but each == and != are overridden for each child class. I had it overridden in the base class, before i made it abstract, but then the base comparison was always called
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 14 Jul, 2008 - 01:57 PM
Post #4


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,933



Thanked 118 times

Dream Kudos: 8525

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Why don't you show us how you're overriding the default == operator, and we will try and help you solve the issue. Without seeing how you're doing it thats just not possible. You will run into issues if the method isnt implemented correctly (either a NullReferenceException or a StackOverFlowException
User is offlineProfile CardPM

Go to the top of the page

redivider
post 15 Jul, 2008 - 07:13 AM
Post #5


New D.I.C Head

*
Joined: 28 May, 2008
Posts: 10

QUOTE(PsychoCoder @ 14 Jul, 2008 - 02:57 PM) *

Why don't you show us how you're overriding the default == operator, and we will try and help you solve the issue. Without seeing how you're doing it thats just not possible.

It has nothing to do with what's in the operation, because its not being called. And i don't think there's anything i can do to change the signiture....but.

CODE
public static bool operator ==(IndustryClassification1 ic1, IndustryClassification1 ic2)
{
if ((object)ic1 == null || (object)ic2 == null)
{
  if ((object)ic1 == null && (object)ic2 == null)
    return true;
  else
    return false;
}
if (ic1.x != ic2.x
     || ic1.y != ic2.y
     || ic1.IsExclude != ic2.IsExclude
     || ic1.Max != ic2.Max
     || ic1.Min != ic2.Min
     || …………………)
  return false;
return true;
}

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:21AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month