Go Back  Xtreme .NET Talk > .NET > Syntax Specific > Visual C# .NET > Operator overloading


Reply
 
Thread Tools Display Modes
  #1  
Old 01-31-2003, 06:40 PM
Morpheus Morpheus is offline
Regular
 
Join Date: Jan 2002
Posts: 62
Morpheus is on a distinguished road
Default Operator overloading

I'm having some problem understanding operator overloading.

I need to overload == to compare if two points have the same coordinates.

Two objects Point1 and Point2 and they have both got two coordinates(X and Y).

I would be really glad if someone could explain two me what I have to do and why.
Reply With Quote
  #2  
Old 01-31-2003, 06:55 PM
Nerseus's Avatar
Nerseus Nerseus is offline
Danner

Preferred language:
C#
 
Join Date: Oct 2002
Location: Arizona, USA
Posts: 2,547
Nerseus is on a distinguished road
Default

If you use the built in type Point (part of System.Drawing) you won't have to overload - they work as expected.

If you just want to see how to implement operator overloading, here's a sample:
Code:
struct MyStruct
{
    public int x;
    public int y;

    public MyStruct(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public static bool operator ==(MyStruct v1, MyStruct v2)
    {
        return (v1.x==v2.x && v1.y==v2.y);
    }
    public static bool operator !=(MyStruct v1, MyStruct v2)
    {
        return (v1.x!=v2.x || v1.y!=v2.y);
    }
}

And here's the sample code to test:
Code:
MyStruct m1 = new MyStruct(1, 2);
MyStruct m2 = new MyStruct(3, 4);
MyStruct m3 = new MyStruct(1, 2);

// The following is true;
if(m1 == m3) Debug.WriteLine("=");
// The following is not true
if(m1 == m2) Debug.WriteLine("=");

-nerseus
__________________
"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Reply With Quote
  #3  
Old 02-01-2003, 04:29 AM
Morpheus Morpheus is offline
Regular
 
Join Date: Jan 2002
Posts: 62
Morpheus is on a distinguished road
Default

Why do you use struct? Can I use have a class instead of struct?
Reply With Quote
  #4  
Old 02-01-2003, 10:20 AM
Volte Volte is offline
Neutiquam Erro

Preferred language:
C# and VB.NET
 
Join Date: Nov 2002
Posts: 2,171
Volte is on a distinguished road
Default

You can use a class if you want; operator overloading will work in
either.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Operator overloading mskeel Random Thoughts 20 03-03-2006 06:47 PM
Operator Overloading in VB 2005 - Dealing with fractions John Tutors Corner 1 01-22-2005 10:44 AM
Overloading? JumpsInLava General 6 04-22-2004 08:53 AM
C# Operator Overloading bri189a Visual C# .NET 4 11-25-2003 03:30 PM
Overloading and Delegates Jarod Visual Basic .NET 4 05-09-2003 05:49 AM

Advertisement:

Powered by liquidweb