-1

Suppose I'm making a utils library, and among other things I have in it a class that involves key input:

public class KeyInput {

  ...

  public bool IsKeyPressed(Keys key) {
    // determine and return whether that key is pressed
  }

  ...

}

The Keys enum is in the standard library, but I'm attempting to make this library so that the user doesn't have to even touch anything outside of it, including the standard library if I can (or at least not to do anything that these utils are intended for). I can use Enum.TryParse() and have the user put in a string according to the what key they want, but it would be much more preferable if I could actually have an enum for it. What I really want is a duplicate of System.Windows.Forms.Keys that has an implicit conversion to it, or alternately some way to make it so that whenever someone uses my namespace, it includes the System.Windows.Forms.Keys enum. Is there any way to do this (or something effectively the same)?

Anonymous
  • 491
  • 2
  • 12
  • 6
    Im confused why somebody using your library can't simply just also use System.Windows.Forms.Keys if they need it. – David Green Mar 17 '17 at 00:52
  • 1
    Key input, from _where_? In this effort to avoid making the client code reference `System.Windows.Forms.dll`, have you also completely reinvented the message loop, keyboard input handling, etc.? If so, then why not just define your own `enum` type explicitly, suited specifically to your own implementation? If not, then your client's code has the reference to that DLL anyway, and you can just use the `Keys` enum. What _problem_ are you actually trying to solve here? Your question doesn't make much sense as-is. – Peter Duniho Mar 17 '17 at 01:54
  • As pointed out by @DavidGreen, users could just use `System.Windows.Forms.Keys`, but my goal is to make it so that when they import my namespace, they don't need to import anything else for my library to be fully functional. As said by @PeterDuniho, I could make my own specialized enum, but that's a lot of typing, and even more to make a conversion from `System.Windows.Forms.Keys` (which I need because my input class listens for key events from a windows form). – Anonymous Mar 17 '17 at 15:14

1 Answers1

0

I can't comment yet, so putting this in here. I understand why you wish to have it in your class/namespace code, so that when someone uses it, they do not have to include any other usings.

Check whether to have an include or using statement inside your namespace, see the differences here: Should 'using' statements be inside or outside the namespace?

Maybe include all functions for the conversion in this class so that if the end user needs to use it, all the functions are handled inside your class.

A couple of options:

  1. So put in all your conversions and functionality that a user could require inside the class, using it almost like a wrapper. Thus defeating the need for the user to include another library.
  2. Have the class output the enum, then if a user uses var output = className.func(); they should not need to include the library.
Community
  • 1
  • 1
Slipoch
  • 750
  • 10
  • 23