overloading operator c# Vector

1.7k views Asked by At

So, I am trying to override the "-" operator in c# to be able to subtract 2 vectors, but my class cannot implement Vector.

namespace Vectors
{
    class VectorUtilv
    {
        private Point _p;
        private Point _p2;
        private Vector _v;


         public Vector V
        {
            get { return _v; }
            set { _v = value; }
        }

        public Point AddVector(Vector v)
        {
            _p.X = (_p.X + v.X);
            _p2.Y = (_p.Y + v.Y);

            return _p2;
        }

        // This is where I am trying to override but I cant add the v.X or 
        // the v.Y because it is not a vector. If i cast it as a vector the 
        // override doesn't work.
        //////////////////////////////////////////////////////////////////
        public static VectorUtilv operator -(Vector a, Vector b)
        {
            Vector v = new Vector();
            v.X = a.X - b.X;
            v.Y = a.Y - b.Y;
            return v;
        }
    }    
}

Any idea how I can remedy this issue?

4

There are 4 answers

0
M.kazem Akhgary On BEST ANSWER

Because you are Trying to define Operator for Class. At least one of its Parameters should be used in Operator with Type of your Class. for example you cant have class Car and define Operator witch only Gets int.

You can't override the operator for existing classes.only your own classes.

If you cant Modify Vector Class then you should declare your own class named Vector. or use the Type of your class for operator.

so you can have

    class VectorUtilv
    {
        private Point _p;
        private Point _p2;
        private Vector _v;
        public static VectorUtilv operator -(VectorUtilv a, VectorUtilv b)
        {
            //...
        }
    }

or

    class Vecotr
    {
        private Point _p;
        private Point _p2;
        private Vector _v;
        public static Vecotr operator -(Vecotr a, Vecotr b)
        { 
            //...
        }
    }

But if you use solution 2. then you need to use qualifiers when using Vector.

System.Windows.Vector // is in Windows assembly
Vector // is your class
0
masx On

Thanks for the great responses. I wish I would I have checked before I figured it out. Would have saved me lots of time. I ended up doing pretty much exactly as you all said. Here is what I have.

public static VectorUtil operator -(VectorUtil aHeroPoint, VectorUtil bEnemyPoint) { VectorUtil v = new VectorUtil(); v.Vec = new Vector(( aHeroPoint._p.X - bEnemyPoint._p.X),( aHeroPoint._p.Y - bEnemyPoint._p.Y)); return v; }

3
SLaks On

You can only override an operator in its own class.

Move all of that code to the Vector class.

0
fsm3xpert On

In your vector class, override the '-' operator in it

public class Vector
{
    public int X { get; set; }
    public int Y { get; set; }

    public static Vector operator -(Vector a, Vector b)
    {
        Vector v = new Vector();
        v.X = a.X - b.X;
        v.Y = a.Y - b.Y;
        return v;
    }
}

Then, you can uses it like that

Vector v1 = new Vector { X = 5, Y = 9};
Vector v2 = new Vector { X = 3, Y = 4 };
Vector vr = v1 - v2;
Console.WriteLine("Resultant Vector X: {0} & Y:{1}", vr.X, vr.Y);

I hope it will help you.