0

I am currently working on a project using OpenTK and ImGui.Net.
Each of which has its own Vector2 class.

I was wondering if it was possible to create my own Vector2 class, which can be used as the other two classes without making conversion methods and can be set using the other classes.

example:

// received class: System.Numerics.Vector2()
myVector2 position = Imgui.GetWindowPosition();

// expected class: OpenTK.Mathematics.Vector2()
OpenTK.Input.Mouse.SetPosition(position.X, position.Y);

In the past I would just make an ".ToOpenTK" and a ".ToImgui" functions to convert my vector2 class to one of the asked classes and make conversion methods to convert them to my vector2 class.

The problem with this is, that I can not easily create a new vector without converting it first. Which just gives it an unnecessary extra level of complexity. It would look something like this:

vector2 position = new(100, 100).ToOpenTK();

Right now my code is a mix of both my own and the actual classes and I would like to have a universal way to use both at once.

tymtam
  • 31,798
  • 8
  • 86
  • 126
GreenData
  • 3
  • 4
  • Depending on what you want to do (and what those classes look like), you have some choices. First use `using` to create aliases for each class (so that you can use them both). Something like `using ImGuiVector2 =ImGui.Net.vector2;` (or whatever the fully qualified name is). Then, if the data the two classes contain is similar, create your own class (or use one of the two of them) and create a way to implement the functionality of both (either by wrapping things or by using extension methods). I can't tell you too much more without knowing more of your problem domain (like the class signatures) – Flydog57 Jan 29 '23 at 00:57
  • 1
    Both `OpenTk` and `ImGui` are stackoverflow tags. You should consider adding them to your question. You're more likely to get someone who knows what you are talking about that way – Flydog57 Jan 29 '23 at 01:06
  • @Flydog57 thanks for your answer. I have quite a lot of scripts, so I would rather not remember to add an alias to every script. (Is it possible to set it once for every script?) The option of creating a new class sounds more practical. Regarding functionality, I only use two methods. A method setting X and Y to Zero and one which changes positive values to negative and the other way around. Besides that, I just need the class to be able to pass and receive the X and Y value as the correct class. (both classes use floats to represent them) – GreenData Jan 29 '23 at 01:14
  • @Flydog57 Thanks for the reminder. I mentioned OpenTK and ImGui so I can use them in the example. There were other similar cases before, it just happens that I decided to ask this question while I was working with those two libraries. – GreenData Jan 29 '23 at 01:18
  • https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators – Sir Rufo Jan 29 '23 at 02:24

1 Answers1

0

I do not mind explicitness and generally I prefer it, but
you could use User-defined explicit and implicit conversion operators.

A toy example:

public readonly record struct MyVector(float X, float Y)
{
    public static implicit operator System.Numerics.Vector2(MyVector v) 
       => new System.Numerics.Vector2.Vector2(x: v.X, y: v.Y);

    public static implicit operator MyVector(System.Numerics.Vector2 b) 
       => new MyVector(X: b.X, Y: b.Y);
}
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • 1
    Thank you for your answer. It took a while to fully understand how it works, but I managed to finally resolve this problem. Thank you ^^ – GreenData Jan 29 '23 at 04:05