0

I'm writing a C# forms game engine and now I am trying add a form of my own vector struct. I have created a working example but it can only take ints, or the data type that I make it to take. But I want to be able to use all of them, ints, floats & decimals but I don't know what to do...

I have tried making a different struct with a different name but that's all clunky and i want to try to keep all of them in a single struct (if it's possible).

public struct Vector2
{
    public int X1;
    public int Y1;

    public Vector2(int x1, int y1)
    {
        X1 = x1;
        Y1 = y1;
    }

    // Another vector2 that takes floats.

    // a third one that takes decimals

}

Any form of help is appreciated! And excuse my English.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
K. Hansson
  • 21
  • 9
  • 1
    Look up `Generics` and `Constraints`. Unfortunately, there is no `numeric` constraint, and one consequence of that is that you can't do arithmetic operations on generic members of a type (see https://stackoverflow.com/questions/10951392/implementing-arithmetic-in-generics and others) – Flydog57 Feb 05 '19 at 21:33
  • 2
    This sounds like an [XY Problem](https://meta.stackexchange.com/questions/66377/). Why do you want to use “ints, floats & decimals”? Those are different data types with different behaviors. – Dour High Arch Feb 05 '19 at 21:40

1 Answers1

0

Or if you want to use different types

public struct<T, U> Vector2
{
    public T X1;
    public U Y1;

    public Vector2(T x1, U y1)
    {
        X1 = x1;
        Y1 = y1;    
    }
}
Christo
  • 292
  • 1
  • 7