2

I would like to know which is the proper practice to follow when need to expose a WinAPI enumeration.

( I think this question affects both C# and VB.Net because just its a member visibility question )

I have a NativeMethods Class as indicates MSDN to follow best practices when P/Invoking, well, inside my NativeMethods I have declared the SetSystemCursor function from Windows API, and also the required Enum for id parameter of SetSystemCursor function.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms648395%28v=vs.85%29.aspx

Then, I did a little wrapper:

Public Shared Function SetSystemCursor(ByVal filePath As String,
                                       ByVal cursorType As NativeMethods.SystemCursorId) As Boolean

    Return NativeMethods.SetSystemCursor(NativeMethods.LoadCursorFromFile(filePath), cursorType)

End Function

Note the cursorType parameter, the problem is that SystemCursorId Enum is not public because as I said is part of the platform invoking definitions inside the NativeMethods class, so, to be able use the wrapper I could do two things, first make public the SystemCursorId Enum ...this means move the Enum outside the private NativeMethods Class like this:

Public Class MyClass

    Private Class NativeMethods

    ' ...

    End Class

    Public Enum SystemCursorId

        Arrow = 32512

        Crosshair = 32515

    End Enum

End Class

Or secondly, re-declare the same Enum outside the NativeMethods class to expose it, something like this:

Public Class MyClass

    Private Class NativeMethods

        Friend Enum SystemCursorId

            Arrow = 32512

            Crosshair = 32515

        End Enum

    End Class

    Public Enum SystemCursorId

        Arrow = NativeMethods.SystemCursorId.Arrow

        Crosshair = NativeMethods.SystemCursorId.Crosshair

    End Enum

End Class

So, in this circunstances, which is the recommended programming practice to follow?.

T.S.
  • 18,195
  • 11
  • 58
  • 78
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 3
    I think this is a matter of opinion. If the .NET API is a straightforward implementation of the native API, then just expose the enums as-is. In general (specifically because you can pass *anything* as an `enum`), I think it's best to validate the .NET args, and translate them to the native API args. In other words, two different `enum`s. This helps to decouple the .NET API from the native. – Jonathon Reinhart Jun 27 '15 at 18:50
  • 2
    +1 for @JonathonReinhart. Look at the `enum`s required for working with files: `FileMode` directly corresponds to the `dwFileDisposition` constants, `FileShare` is basically those constants, but some are added, and `FileAccess` is entirely translated since none of the values match. – Mitch Jun 27 '15 at 19:33

0 Answers0