title: VB.NET: "AddHandler" by specifying Handler function as a variable
body: <p>I'm writing an add-in for Autodesk Inventor (a 3D CAD program). The add-in adds a bunch of internal commands to Inventor, and creates a toolbar button for each command. I need to connect the Execute event for each of those commands to a handler function in my add-in.</p>
  
  <p>I've created a "MyCommand" class, which contains all of the information necessary to define the command. I've also created a List(Of MyCommand) with the definitions for each command. Finally, I can iterate through each command and create the internal Inventor command definition, as well as add the button to the toolbar. However, what I can't figure out how to do is associate the command with its Handler function <em>within the loop</em>.</p>
  
  <p>The example code below illustrates what I'm after:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Sub CreateCommands()
      Dim oCommands As New List(Of MyCommand)
  
      oCommands.Add(New MyCommand("DrawLogoCmd", "Draw Logo", "DrawLogoSub"))
      oCommands.Add(New MyCommand("PublishDrawingCmd", "Publish Drawing", "PublishDrawingSub"))
      ' [Dozens more commands]
  
      For Each oCommand As MyCommand In oCommands
          ' Code for adding internal command definition and button to Inventor. This is working fine.
          Dim oCommandDef As Inventor.CommandDefinition = InventorApp.CommandDefinitions.Add(oCommand.InternalDefinitionName, oCommand.DisplayName)
          InventorApp.ToolbarButtons.Add(oCommandDef)
  
          ' Associate command definition with handler function
          ' ===== THIS IS THE LINE I NEED TO FIGURE OUT =====
          AddHandler oCommandDef.OnExecute, AddressOf oCommand.HandlerSubName
      Next
  End Sub
  
  Sub DrawLogoSub()
      ' [My add-in's code to draw logo in Inventor]
  End Sub
  
  Sub PublishDrawingSub()
      ' [My add-in's code to publish drawing in Inventor]
  End Sub
  
  Class MyCommand
      Public InternalDefinitionName As String
      Public DisplayName As String
      Public HandlerSubName As String
  
      Sub New(InternalDefinitionName As String, DisplayName As String, HandlerSubName As String)
          With Me
              .InternalDefinitionName = InternalDefinitionName
              .DisplayName = DisplayName
              .HandlerSubName = HandlerSubName
          End With
      End Sub
  End Class
  </code></pre>
  
  <p>So, is this possible? Is there some way to get the "AddressOf" my function using its name as a string? Or, is there some way to store a reference to the function itself in my MyCommand class, and pass that to the AddressOf operator?</p>
  
  <p>Or, is there some other way besides "AddHandler/AddressOf" that might work?</p>
  
  <p>Any suggestions are appreciated.</p>