7

When defining Windows API const values, is it better to have them as const

public const int SW_HIDE = 0;
public const int SW_SHOWNORMAL = 1;
public const int SW_NORMAL = 1;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_MAXIMIZE = 3;
public const int SW_SHOWNOACTIVATE = 4;
public const int SW_SHOW = 5;
public const int SW_MINIMIZE = 6;
public const int SW_SHOWMINNOACTIVE = 7;
public const int SW_SHOWNA = 8;
public const int SW_RESTORE = 9;
public const int SW_SHOWDEFAULT = 10;
public const int SW_MAX = 10;

[DllImport( "user32.dll" )]
public static extern bool ShowWindow( HandleRef hWnd, int nCmdShow );

or to group them together as an enum.

public enum SW {
  SW_HIDE = 0,
  SW_SHOWNORMAL = 1,
  SW_NORMAL = 1,
  SW_SHOWMINIMIZED = 2,
  SW_SHOWMAXIMIZED = 3,
  SW_MAXIMIZE = 3,
  SW_SHOWNOACTIVATE = 4,
  SW_SHOW = 5,
  SW_MINIMIZE = 6,
  SW_SHOWMINNOACTIVE = 7,
  SW_SHOWNA = 8,
  SW_RESTORE = 9,
  SW_SHOWDEFAULT = 10,
  SW_MAX = 10
}

[DllImport( "user32.dll" )]
public static extern bool ShowWindow( HandleRef hWnd, SW nCmdShow );
Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
JDMX
  • 1,489
  • 9
  • 22
  • I prefer the `enum` version you showed, but without the redundant prefix on the names: `public enum SW { HIDE = 0, SHOWNORMAL = 1 ... etc.`. At a minimum, to observe [DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself) and eliminate (the possible existence of) a whole class of bug or confusion (`SW.SX_MODE == ???`) – Glenn Slayden Feb 07 '17 at 00:12

2 Answers2

6

Group them as enums.

Why? Ints are used all over the place and you can pass them where, for example, a size is necessary as well.. That led to the frickin hungarian notation (szSomething..) in the first place. The type system was lacking and they tried to "fix" it using the variable naming scheme. You're now better off, with a better type system; use it.

Define enums, group them in a sensible way and you won't Thread.Sleep(WM_User) someday (Yes, I'm not completely serious with this example, but I think you get the point).

Ryan Emerle
  • 15,461
  • 8
  • 52
  • 69
Benjamin Podszun
  • 9,679
  • 3
  • 34
  • 45
5

Except for code maintainability, it doesn't matter at all.

I recommend using an enum; this allows you to use IntelliSense when calling the function, and can help prevent mistakes.
However, you should give your enum a meaningful name, such as WindowShowType.
Also, you might want to remove the prefixes, and perhaps standardize the names to CamelCase.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • While I agree with removing the prefixes, I prefer to keep the ALLCAPS names from `Win32`, rather than switching them **TitleCase** as you suggest (I usually reserve the term **camelCase** for the form I just illustrated it in). My reasoning is that the former change has a principled motivation (see my other comment on this page), while the latter seems more gratuitous, possibly even losing information--albeit circumstantial--about the Win32 provenance of the enum. – Glenn Slayden Feb 07 '17 at 00:23