Welcome to Dream.In.Code
Become a C# Expert!

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




overloading binary operator

 
Reply to this topicStart new topic

overloading binary operator

jayraj
5 Mar, 2008 - 08:03 AM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 9


My Contributions
hey guys m lookin doin dis simple example on binary operators but its not workin
d error its showing is
'Space.operator Space ' cannt return a value
and only assgnment call such as increment and dcreement can return a value
guys plz help
need to get d concept clear
m a beginner in c#

csharp

using System;

class Space
{
int x,y,z;

public Space(int a, int b,int c)
{
x=a;
y=b;
z=c;
}
public void display()
{

Console.Write(""+x);
Console.Write(""+y);
Console.Write(""+z);
Console.WriteLine();
}

public static Space operator -( Space s)
{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}

}

class SpaceTest
{
public static void Main()
{
Space s=new Space(-10,20,30);

Console.Write("s:");
s.display();

-s;

Console.Write("s:");
s.display();
}
}


This post has been edited by PsychoCoder: 5 Mar, 2008 - 08:09 AM
User is offlineProfile CardPM
+Quote Post

Nykc
RE: Overloading Binary Operator
5 Mar, 2008 - 08:04 AM
Post #2

That Just Happened!
Group Icon

Joined: 14 Sep, 2007
Posts: 4,509



Thanked: 18 times
Dream Kudos: 275
My Contributions
Please use code.gif

So we can read it better
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: Overloading Binary Operator
5 Mar, 2008 - 08:22 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Try changing this code


csharp

public static Space operator -( Space s)
{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}



To this, I believe this will resolve your issue


csharp

public static Space operator -(Space s1, Space s2)
{
Space result = new Space();
result.x = s1.x - s2.x;
result.y = s1.y - s2.y;
return result;
}



Hope that helps smile.gif
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: Overloading Binary Operator
5 Mar, 2008 - 08:29 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Oops, forgot your 3rd variable, try this

csharp

public static Space operator -(Space s1, Space s2, Space s3)
{
Space result = new Space();
result.x = s1.x - s2.x - s3.x;
result.y = s1.y - s2.y - s3.y;
result.z = s1.z - s2.z - s3.z;
return result;
}


This post has been edited by PsychoCoder: 5 Mar, 2008 - 08:29 AM
User is online!Profile CardPM
+Quote Post

baavgai
RE: Overloading Binary Operator
5 Mar, 2008 - 10:50 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,290



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Fun example, thanks!

The operator you're overriding here is "-". However, from your example, it looks like you're really thinking "--". The "-" operator should return a value, but not affect the object it's working on. The incrementors will affect the object.

Taking your example, this might be clearer.

csharp

using System;

class Space {
public int x, y, z;
public Space(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}

// rather than a display method, I'm going to override the ToString method.
public override string ToString() {
return string.Format("({0},{1},{2})", x, y, z);
}

// this is the - operator
//note that nothing happens to the object passed
public static Space operator -(Space s) {
return new Space(-s.x, -s.y, -s.z);
}

// this is the -- operator
// note that we operate on the passed value
// and then pass it back.
public static Space operator --(Space s) {
s.x = -s.x;
s.y = -s.y;
s.z = -s.z;
return s;
}
}


class SpaceTest {
public static void Main() {
Space s = new Space(-10, 20, 30);

// starting value
Console.WriteLine(s); // prints (-10,20,30)

// value is returned
Console.WriteLine(-s); // prints (10,-20,-30)

// but doesn't affect the object
Console.WriteLine(s);// prints (-10,20,30)

// this does affect the object
--s;
Console.WriteLine(s);// prints (10,-20,-30)
}
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month