My question is pretty simple, but I didn't find a way to implement my code the way I want it to be. So I started wondering if the code I want to implement is not good. And if it is, what's the best way to do it.
Here it goes:
class InputManager
{
SortedDictionary<ushort,Keys> inputList = new SortedDictionary<ushort,Keys>();
public void Add(ushort id, Keys key) {...}
public bool IsPressed(ushort id) {...}
}
class Main
{
private enum RegisteredInput : ushort
{
Up,
Down,
Confirm
}
public Main()
{
InputManager manager = new InputManager();
manager.Add(RegisteredInput.Up, Keys.Q);
manager.Add(RegisteredInput.Down, Keys.A);
manager.Add(RegisteredInput.Confirm, Keys.Enter);
}
void update()
{
if(manager.IsPressed(RegisteredInput.Up)) action();
}
}
This code won't compile, giving errors of this kind:
The best overloaded method match for 'InputManager.Add(ushort, Keys)' has some invalid arguments
Argument '1': cannot convert from 'RegisteredInput' to 'ushort'
If I use a cast like in manager.Add((ushort)RegisteredInput.Up, Keys.Q); it will work. But because the cast must be explicit, I was wondering if it is not recomended code in C# like it is in C++ and if there is a better way of doing it (like using const ushort for every value, which I kinda don't like much).
The best answer I got so far was from this thread, but it sounds so much like a hack, I got worried.
Thanks!
Dictionary<RegisteredInput, Keys>? Also, try to avoid retyping error messages and code into websites when you ask about things, the chance that you do something wrong we will get hung up on is there. Like... did the exception message actually name the method "Addd" with 3 d's? How can we trust you to actually copy the actual code used, and not retype something simplified that has other problems altogether?