title: Code Analysis Warning about a Constructor
body: <p>I've written a class to perform system-wide hotkeys operations and this is the only constructor:</p>
  
  <pre><code>''' &lt;summary&gt;
  ''' Creates a new system-wide hotkey.
  ''' &lt;/summary&gt;
  ''' &lt;param name="Modifier"&gt;The keys that must be pressed in combination with the specified key.&lt;/param&gt;
  ''' &lt;param name="Key"&gt;The key used for the hotkey.&lt;/param&gt;
  ''' &lt;exception cref="IsRegisteredException"&gt;&lt;/exception&gt;
  &lt;DebuggerStepperBoundary()&gt;
  Public Sub New(ByVal Modifier As HotkeyModifier, ByVal Key As Keys)
  
      Me.CreateHandle(New CreateParams)
  
      Me.PressEventArgs.ID = GetHashCode()
      Me.PressEventArgs.Key = Key
      Me.PressEventArgs.Modifier = Modifier
  
      If Not NativeMethods.RegisterHotKey(Me.Handle,
                                          Me.PressEventArgs.ID,
                                          Me.PressEventArgs.Modifier,
                                          Me.PressEventArgs.Key) Then
  
          Throw New IsRegisteredException
  
      End If
  
  End Sub
  </code></pre>
  
  <p>And a code analysis on the project says this:</p>
  
  <blockquote>
    <p>CA2214    Do not call overridable methods in
    constructors  'GlobalHotkey.New(GlobalHotkey.HotkeyModifier, Keys)'
    contains a call chain that results in a call to a virtual method
    defined by the class. Review the following call stack for unintended
    consequences: </p>
    
    <p>GlobalHotkey..ctor(GlobalHotkey+HotkeyModifier, Keys)
    NativeWindow.CreateHandle(CreateParams):Void
    Object.GetHashCode():Int32    Color .NET  GlobalHorkey.vb 213</p>
  </blockquote>
  
  <p>PS: The line 213 is this:</p>
  
  <pre><code>Public Sub New(ByVal Modifier As HotkeyModifier, ByVal Key As Keys)
  </code></pre>
  
  <p>What I need to do to avoid this conflict?</p>
  
  <blockquote>
    <p>UPDATE</p>
  </blockquote>
  
  <p>Into the NativeMethods class... :</p>
  
  <pre><code>    &lt;DllImport("user32.dll", SetLastError:=True)&gt;
      Public Shared Function RegisterHotKey(
                    ByVal hWnd As IntPtr,
                    ByVal id As Integer,
                    ByVal fsModifiers As UInteger,
                    ByVal vk As UInteger
      ) As &lt;MarshalAs(UnmanagedType.Bool)&gt; Boolean
      End Function
  </code></pre>
  
  <blockquote>
    <p>UPDATE 2</p>
  </blockquote>
  
  <p>This is the full Class:</p>
  
  <pre><code>#Region " Usage Examples "
  
  'Public Class Form1
  
  '    ''' &lt;summary&gt;
  '    ''' Stores the system-wide hotkey object.
  '    ''' &lt;/summary&gt;
  '    Private WithEvents Hotkey As GlobalHotkey = Nothing
  
  '    ''' &lt;summary&gt;
  '    ''' Initializes a new instance of the &lt;see cref="Form1"/&gt; class.
  '    ''' &lt;/summary&gt;
  '    Public Sub New()
  
  '        InitializeComponent()
  
  '        ' Registers a new global hotkey on the system. (Alt + Ctrl + A)
  '        Hotkey = New GlobalHotkey(GlobalHotkey.HotkeyModifier.Alt Or GlobalHotkey.HotkeyModifier.Ctrl, Keys.A)
  
  '        ' Replaces the current registered hotkey with a new global hotkey on the system. (Alt + Escape)
  '        Hotkey = New GlobalHotkey([Enum].Parse(GetType(GlobalHotkey.HotkeyModifier), "Alt", True),
  '                                  [Enum].Parse(GetType(Keys), "Escape", True))
  
  '    End Sub
  
  '    ''' &lt;summary&gt;
  '    ''' Handles the Press event of the HotKey control.
  '    ''' &lt;/summary&gt;
  '    Private Sub HotKey_Press(ByVal sender As Object, ByVal e As GlobalHotkey.HotKeyEventArgs) _
  '    Handles Hotkey.Press
  
  '        MsgBox(e.ID)
  '        MsgBox(e.Key.ToString)
  '        MsgBox(e.Modifier.ToString)
  
  '    End Sub
  
  'End Class
  
  #End Region
  
  #Region " Imports "
  
  Imports System.Runtime.InteropServices
  
  #End Region
  
  #Region " Global Hotkey "
  
  ''' &lt;summary&gt;
  ''' Class to perform system-wide hotkey operations.
  ''' &lt;/summary&gt;
  Class GlobalHotkey
      Inherits NativeWindow
      Implements IDisposable
  
  #Region " API "
  
      ''' &lt;summary&gt;
      ''' Native API Methods.
      ''' &lt;/summary&gt;
      Public Class NativeMethods
  
          ''' &lt;summary&gt;
          ''' Defines a system-wide hotkey.
          ''' &lt;/summary&gt;
          ''' &lt;param name="hWnd"&gt;The hWND.&lt;/param&gt;
          ''' &lt;param name="id"&gt;The identifier of the hotkey.
          ''' If the hWnd parameter is NULL, then the hotkey is associated with the current thread rather than with a particular window.
          ''' If a hotkey already exists with the same hWnd and id parameters.&lt;/param&gt;
          ''' &lt;param name="fsModifiers"&gt;The keys that must be pressed in combination with the key specified by the uVirtKey parameter
          ''' in order to generate the WM_HOTKEY message.
          ''' The fsModifiers parameter can be a combination of the following values.&lt;/param&gt;
          ''' &lt;param name="vk"&gt;The virtual-key code of the hotkey.&lt;/param&gt;
          ''' &lt;returns&gt;
          ''' &lt;c&gt;true&lt;/c&gt; if the function succeeds, otherwise &lt;c&gt;false&lt;/c&gt;
          ''' &lt;/returns&gt;
          &lt;DllImport("user32.dll", SetLastError:=True)&gt;
          Public Shared Function RegisterHotKey(
                        ByVal hWnd As IntPtr,
                        ByVal id As Integer,
                        ByVal fsModifiers As UInteger,
                        ByVal vk As UInteger
          ) As &lt;MarshalAs(UnmanagedType.Bool)&gt; Boolean
          End Function
  
          ''' &lt;summary&gt;
          ''' Unregisters a hotkey previously registered.
          ''' &lt;/summary&gt;
          ''' &lt;param name="hWnd"&gt;The hWND.&lt;/param&gt;
          ''' &lt;param name="id"&gt;The identifier of the hotkey to be unregistered.&lt;/param&gt;
          ''' &lt;returns&gt;
          ''' &lt;c&gt;true&lt;/c&gt; if the function succeeds, otherwise &lt;c&gt;false&lt;/c&gt;
          ''' &lt;/returns&gt;
          &lt;DllImport("user32.dll", SetLastError:=True)&gt;
          Public Shared Function UnregisterHotKey(
                        ByVal hWnd As IntPtr,
                        ByVal id As Integer
          ) As &lt;MarshalAs(UnmanagedType.Bool)&gt; Boolean
          End Function
  
      End Class
  
  #End Region
  
  #Region " Members "
  
      ''' &lt;summary&gt;
      ''' The hotkey modifier used in combination with the key.
      ''' &lt;/summary&gt;
      &lt;Flags&gt;
      Public Enum HotkeyModifier As Integer
  
          ''' &lt;summary&gt;
          ''' The none
          ''' &lt;/summary&gt;
          None = &amp;H0
  
          ''' &lt;summary&gt;
          ''' The alt key
          ''' &lt;/summary&gt;
          Alt = &amp;H1
  
          ''' &lt;summary&gt;
          ''' The control key
          ''' &lt;/summary&gt;
          Ctrl = &amp;H2
  
          ''' &lt;summary&gt;
          ''' The shift key
          ''' &lt;/summary&gt;
          Shift = &amp;H4
  
          ''' &lt;summary&gt;
          ''' The win key
          ''' &lt;/summary&gt;
          Win = &amp;H8
  
      End Enum
  
      ''' &lt;summary&gt;
      ''' Event that is raised when a hotkey is pressed.
      ''' &lt;/summary&gt;
      Public Event Press As EventHandler(Of HotKeyEventArgs)
  
      ''' &lt;summary&gt;
      ''' Stores the Press Event Arguments.
      ''' &lt;/summary&gt;
      Protected PressEventArgs As New HotKeyEventArgs
  
      ''' &lt;summary&gt;
      ''' Event arguments for the Press event.
      ''' &lt;/summary&gt;
      Public Class HotKeyEventArgs : Inherits EventArgs
  
          ''' &lt;summary&gt;
          ''' The identifier of the hotkey.
          ''' &lt;/summary&gt;
          ''' &lt;value&gt;The identifier.&lt;/value&gt;
          Public Property ID As Integer
  
          ''' &lt;summary&gt;
          ''' The key used for the hotkey.
          ''' &lt;/summary&gt;
          ''' &lt;value&gt;The key.&lt;/value&gt;
          Public Property Key As Keys
  
          ''' &lt;summary&gt;
          ''' The key modifier used for the hotkey.
          ''' &lt;/summary&gt;
          ''' &lt;value&gt;The modifier.&lt;/value&gt;
          Public Property Modifier As HotkeyModifier
  
      End Class
  
      ''' &lt;summary&gt;
      ''' Exception that is thrown when a hotkey is already registered.
      ''' &lt;/summary&gt;
      &lt;Serializable&gt;
      Public Class IsRegisteredException : Inherits Exception
  
          ''' &lt;summary&gt;
          ''' Initializes a new instance of the &lt;see cref="IsRegisteredException"/&gt; class.
          ''' &lt;/summary&gt;
          Sub New()
              MyBase.New("Hotkey combination is already in use.")
          End Sub
  
      End Class
  
  #End Region
  
  #Region " Constructor "
  
      ''' &lt;summary&gt;
      ''' Creates a new system-wide hotkey.
      ''' &lt;/summary&gt;
      ''' &lt;param name="Modifier"&gt;The keys that must be pressed in combination with the specified key.&lt;/param&gt;
      ''' &lt;param name="Key"&gt;The key used for the hotkey.&lt;/param&gt;
      ''' &lt;exception cref="IsRegisteredException"&gt;&lt;/exception&gt;
      &lt;DebuggerStepperBoundary()&gt;
      Public Sub New(ByVal Modifier As HotkeyModifier, ByVal Key As Keys)
  
          Me.CreateHandle(New CreateParams)
  
          Me.PressEventArgs.ID = GetHashCode()
          Me.PressEventArgs.Key = Key
          Me.PressEventArgs.Modifier = Modifier
  
          If Not NativeMethods.RegisterHotKey(Me.Handle,
                                              Me.PressEventArgs.ID,
                                              Me.PressEventArgs.Modifier,
                                              Me.PressEventArgs.Key) Then
  
              Throw New IsRegisteredException
  
          End If
  
      End Sub
  
  #End Region
  
  #Region " WndProc "
  
      ''' &lt;summary&gt;
      ''' Invokes the default window procedure associated with this window.
      ''' &lt;/summary&gt;
      ''' &lt;param name="m"&gt;
      ''' A &lt;see cref="T:System.Windows.Forms.Message" /&gt; that is associated with the current Windows message.
      ''' &lt;/param&gt;
      Protected Overrides Sub WndProc(ByRef m As Message)
  
          If m.Msg = 786 Then
              RaiseEvent Press(Me, Me.PressEventArgs)
          Else
              MyBase.WndProc(m)
          End If
  
      End Sub
  
  #End Region
  
  #Region " IDisposable "
  
      ''' &lt;summary&gt;
      ''' To detect redundant calls when disposing.
      ''' &lt;/summary&gt;
      Private IsDisposed As Boolean = False
  
      ''' &lt;summary&gt;
      ''' Prevent calls to methods after disposing.
      ''' &lt;/summary&gt;
      ''' &lt;exception cref="System.ObjectDisposedException"&gt;&lt;/exception&gt;
      Private Sub DisposedCheck()
  
          If Me.IsDisposed Then
              Throw New ObjectDisposedException(Me.GetType().FullName)
          End If
  
      End Sub
  
      ''' &lt;summary&gt;
      ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
      ''' &lt;/summary&gt;
      Public Sub Dispose() Implements IDisposable.Dispose
          Dispose(True)
          GC.SuppressFinalize(Me)
      End Sub
  
      ' IDisposable
      ''' &lt;summary&gt;
      ''' Releases unmanaged and - optionally - managed resources.
      ''' &lt;/summary&gt;
      ''' &lt;param name="IsDisposing"&gt;&lt;c&gt;true&lt;/c&gt; to release both managed and unmanaged resources; &lt;c&gt;false&lt;/c&gt; to release only unmanaged resources.&lt;/param&gt;
      Protected Overridable Sub Dispose(IsDisposing As Boolean)
  
          If Not Me.IsDisposed Then
  
              If IsDisposing Then
                  NativeMethods.UnregisterHotKey(Me.Handle, Me.PressEventArgs.ID)
              End If
  
          End If
  
          Me.IsDisposed = True
  
      End Sub
  
  #End Region
  
  End Class
  
  #End Region
  </code></pre>