Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Problem with operator

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
boeske@sf-systemtechnik.de - 26 Jun 2007 07:48 GMT
Hello,

I have the following C++-Code:

Definition:

class Klasse
{
public:
    Klasse operator+(Klasse Op);

public:
    int X;
};

Implementation:

Klasse Klasse::operator+(Klasse Op)
{
    Klasse Result;

    Result.X = this->X + Op.X;

    return Result;
};

I want to transfer it to C#, like this:

class Klasse
{
   public int X;

   public static Klasse operator +(Klasse Op) // 'static' is
obligatory!
   {
       MVector Result;
       Result = new Result();

       Result.X = this.X + Op.X; //<- but this doesn't work in
'static'!

       return Result;
   }
}

The C#-Code is not working, because:

1. operator-methods have to be declared as static
2. static methoden my not use 'this'.

Unfortunately I don't find a solution. Can anybody give me a hint?

Thanks,
Lars
Jon Skeet [C# MVP] - 26 Jun 2007 08:06 GMT
On Jun 26, 7:48 am, boe...@sf-systemtechnik.de wrote:

<snip>

> The C#-Code is not working, because:
>
> 1. operator-methods have to be declared as static
> 2. static methoden my not use 'this'.
>
> Unfortunately I don't find a solution. Can anybody give me a hint?

You need to make your operator overload take *two* instances, not just
one:

public static Klasse operator +(Klasse first, Klasse second)
   {
       MVector Result;
       Result = new Result();

       Result.X = first.X + second.X;

       return Result;
   }

(Did you mean to make the type of Result Klasse rather than either
Result or MVector, by the way?)

Jon
Alberto Poblacion - 26 Jun 2007 08:10 GMT
> The C#-Code is not working, because:
>
> 1. operator-methods have to be declared as static
> 2. static methoden my not use 'this'.
>
> Unfortunately I don't find a solution. Can anybody give me a hint?

  Operator redefinition is different in C#. In C++ the operator is an
instance method and it operates between the "this" and the received
argument. In C#, the operators are static, and they receive as arguments the
two parameters on which they operate:

public static Klasse operator+(Klasse t1, Klasse t2)
{
       Klasse result;
       result = new Klasse();
       result.X = t1.X + t2.X;
       return result;
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.