Shared/Static CommandMethods

By Stephen Preston

If you set your CommandMethods as normal class instance members:

Public Class MyCommands
     _
    Public Sub MyCommand()
      'My code
    End Sub
  End Class

then AutoCAD will automatically instantiate your class when your command is called in a new document. However, if you make your CommandMethod a Shared (static in C#) method, then AutoCAD can call your method without instantiating your class:

Public Class MyCommands
     _
    Public Shared Sub MyCommand()
      'My code
    End Sub
  End Class

You can test this by writing a constructor for your class and watching when its called.


Comments

3 responses to “Shared/Static CommandMethods”

  1. David Avatar
    David

    Hi Stephen,
    Sorry I am not good in English , Which one is better ?
    and basically what is the meaning of instantiate ?
    Sorry for my low level question .

  2. Neither is better or worse – it depends on the architecture you’re implementing. I tend to use the shared/static approach for simple apps – but for no particular reason.
    ‘Instantiate’ means ‘to create an instance of that class’.

  3. David Avatar
    David

    Thank you Stephen

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading