Quick Tip to set Class Name parameter of Ribbon Items

By Augusto Goncalves

When we create new Ribbon items to execute a class that implements IExternalCommand, we need to specify this class name on the constructor of the  item (e.g. PushButtonData). The thing is that parameter is a String and if we use a constant text, if the class name changes, then we need to remember to adjust this value.

So how can we make this more efficient? Simply using GetType (VB.NET) or typeof (C#). The benefit is the value adjust automatically (Visual Studio renaming mechanism will update it properly) or at least we get and compiler error to help. Of course, this will not be applicable if we’re pointing to a class on a different and not referenced assembly.

VB.NET

Dim pbdNewButton As New PushButtonData( _

  "BTN_NEW_BUTTON", _

  "New Button Text", _

  System.Reflection.Assembly.GetExecutingAssembly().Location, _

  GetType(ClassName).FullName)

C#

PushButtonData pbdNewButton = new PushButtonData(

  "BTN_NEW_BUTTON",

  "New Button Text",

  System.Reflection.Assembly.GetExecutingAssembly().Location,

  typeof(ClassName).FullName);


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading