I'm wondering how to make my custom user type able to be added and subtracted from the same types. For example:

public class CustomType {
public float x;
public float y;

public CustomType(float x, float y) {
this.x = x;
this.y = y; } }

public class test : Form {
CustomType r = new CustomType(3, 5);

private void Form1_Load(object sender, EventArgs e) {
MessageBox.Show(r.ToString() + " squared is: " + Square(r).ToString()); }

public CustomType Square(CustomType type) {
return type * type; } }

As you all know this will have a single syntax error which is simply that the operator '*' can't be applied to operands of 'CustomType' and 'CustomType'. So I simply want some information on how I can achieve this with my custom types like XNA Framework's Vector2 class. I can multiply it, subtract it, add it, and divide it with by it's own type. So any information would be helpful.

Thanks,
Jamie

What you are talking about is operator overloading.

Let's say you want to define '+' for your type to mean that the x values are added together, and the y values are added together. You'd do something like this:

public static CustomType operator +(CustomType r1, CustomType r2) {
            return new CustomType(r1.x + r2.x, r1.y + r2.y);
}

Now you can add these together!

You can also overload it using types other than your type. For example, say you want '*' for an integer to mean 'multiply both x and y by the value':

public static CustomType operator *(CustomType r1, int n) {
            return new CustomType(r1.x * n, r1.y * n;
}

Note: Some of the operators have to be done in pairs, for example you can't do '<' without doing '>'.
Note 2: Be careful when you overload operators. While it might seem neat to you to overload '+' on an image to mean combine the average of the pixel colors together, it won't be clear to someone reading your code what exactly image1 + image2 means. Try to keep the meaning obvious.

Edited 4 Years Ago by Momerath: n/a

So say I want to overload an operator to say raise a type to a power could I replace the following method with the second?:

// Regular Method
public Size2 ToPower(Size2 Dimensions, int Exponent) {
float w = Dimensions.width;
float h = Dimensions.height;

for (int i = 1; i < Exponent; i++) {
Dimensions.width *= w;
Dimensions.height *= h; }

return Dimensions; }

// Overload
public static Size2 operator ^(Size2 Dimensions, int Exponent) {
float w = Dimensions.Width;
float h = Dimensions.Height;

for (int i = 1; i < Exponent; i++) {
Dimensions.width *= w;
Dimensions.height *= h; }

return new Size2(Dimensions.width, Dimensions.height); }

Is this even possible? I know that the '^' operator is I believe what is called the pointer operator, not 100% sure because I haven't looked at it in a while, but it's an operator that didn't give me trouble. I haven't tested the method or the overload but just by doing a run through of the loop and everything it should return the correct values. I'm just wondering if overloading an operator like this is possible? Other than that my question was answered.

public static Size2 operator +(Size2 LeftOperand, Size2 RightOperand) {
            return new Size2(LeftOperand.Width + RightOperand.Width, LeftOperand.Height + RightOperand.Height);
        }

        public static Size2 operator -(Size2 LeftOperand, Size2 RightOperand) {
            return new Size2(LeftOperand.Width - RightOperand.Width, LeftOperand.Height - RightOperand.Height);
        }

        public static Size2 operator *(Size2 LeftOperand, Size2 RightOperand) {
            return new Size2(LeftOperand.Width * RightOperand.Width, LeftOperand.Height * RightOperand.Height);
        }

        public static Size2 operator /(Size2 LeftOperand, Size2 RightOperand) {
            return new Size2(LeftOperand.Width / RightOperand.Width, LeftOperand.Height / RightOperand.Height);
        }

        public static Size2 operator %(Size2 LeftOperand, Size2 RightOperand) {
            return new Size2(LeftOperand.Width % RightOperand.Width, LeftOperand.Height % RightOperand.Height);
        }

Thanks,
Jamie

In C# ^ means XOR by default (to datatypes that support it, anyway) but I don't see why you couldn't overload it to make it a power.

Edited 4 Years Ago by skatamatic: n/a

Nevermind, it worked. I tested it with a forms app and it works just fine. Thank you, this opens a whole new world for me to explore. :)

Another interesting feature to explore is type overloading (that's what I call it anyway, for lack of a better term)

public static implicit operator string(MyClass RHS)
        {
            return RHS.ToString();
        }

Now MyClass can be implicitly converted to a string:

MyClass someClass = new MyClass();
string myString = "Some string" + someClass; //no compile error!

I can see a lot of bad coming out of using this if it isn't used properly though....

This question has already been answered. Start a new discussion instead.