Operator Definition Question

G

Gene Wirchenko

Dear CSharpies:

Suppose that I have two classes. Call them One and Two. I wish
to define addition between them.

For the static version, I can code

class One
{
...
public static ResultType operator +(One LHS, Two RHS)
{
// Addition definition goes here.
}
...

but I can also do this for Two. Which should I pick? What if I do it
for both?

A pointer to what I should and should not do, please.

Sincerely,

Gene Wirchenko
 
A

Arne Vajhøj

Suppose that I have two classes. Call them One and Two. I wish
to define addition between them.

For the static version, I can code

class One
{
...
public static ResultType operator +(One LHS, Two RHS)
{
// Addition definition goes here.
}
...

but I can also do this for Two. Which should I pick? What if I do it
for both?

A pointer to what I should and should not do, please.

I would define + for one of them and conversion for the other.

Example:

using System;

namespace E
{
public class One
{
public int Val { get; set; }
public static One operator+(One v1, One v2)
{
return new One { Val = v1.Val + v2.Val };
}
public override string ToString()
{
return Val.ToString();
}
}
public class Two
{
public int Val { get; set; }
public static implicit operator One(Two v)
{
return new One { Val = v.Val };
}
}
public class Program
{
public static void Main(string[] args)
{
One a = new One { Val = 123 };
One b = new One { Val = 456 };
Console.WriteLine(a + b);
Two c = new Two { Val = 456 };
Console.WriteLine(a + c);
Console.ReadKey();
}
}
}

Arne
 
A

Arne Vajhøj

Suppose that I have two classes. Call them One and Two. I wish
to define addition between them.

For the static version, I can code

class One
{
...
public static ResultType operator +(One LHS, Two RHS)
{
// Addition definition goes here.
}
...

but I can also do this for Two. Which should I pick? What if I do it
for both?

A pointer to what I should and should not do, please.

I would define + for one of them and conversion for the other.

Example:

using System;

namespace E
{
public class One
{
public int Val { get; set; }
public static One operator+(One v1, One v2)
{
return new One { Val = v1.Val + v2.Val };
}
public override string ToString()
{
return Val.ToString();
}
}
public class Two
{
public int Val { get; set; }
public static implicit operator One(Two v)
{
return new One { Val = v.Val };
}
}
public class Program
{
public static void Main(string[] args)
{
One a = new One { Val = 123 };
One b = new One { Val = 456 };
Console.WriteLine(a + b);
Two c = new Two { Val = 456 };
Console.WriteLine(a + c);
Console.ReadKey();
}
}
}

It is similar to what is done or the builtin
operators/types.

Arne
 
R

Registered User

Dear CSharpies:

Suppose that I have two classes. Call them One and Two. I wish
to define addition between them.

For the static version, I can code

class One
{
...
public static ResultType operator +(One LHS, Two RHS)
{
// Addition definition goes here.
}
...

but I can also do this for Two. Which should I pick? What if I do it
for both?

A pointer to what I should and should not do, please.
I would not recommended using an overloaded operator in this situation
simply because it introduces a dependency between the types One and
Two. Abstractly the method outlined above takes an argument of type
One and an argument of type Two with its return value being an object
of type ResultType. That sounds like a c'tor.

class ResultType
{
public ResultType(One lhs, Two rhs)
{
...
}
...
}

regards
A.G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top