Tick Talk on EsoxRepublic.com


Early Binding Speeds API Programming

Posted in API by Administrator on the July 24th, 2008

One of the challenges when writing API code is keeping track of objects and their methods and properties. Early binding can help with that.

“Early binding” simply refers to the way that object variables are defined. Instead of defining an object variable with “Dim swApp as Object”, define it as the specific object type, i.e. “Dim swApp as SldWorks.SldWorks”.

Here is what SW gives you when you record a macro:

Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object

This is what the same code would look like adjusted for early binding:

Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim SelMgr As SelectionMgr
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As SldWorks.Feature

Ctrl+J and Intellisense

One of the handy features in the VBA editor (macro editor) is a list of available commands that pops up when typing, known as “intellisense”. Details can be found in the VBA help under “List Properties/Methods Command (Edit Menu)”. With intellisense, there is no need to commit an endless list of commands, objects, methods and properties to memory. It’s all right there.

Screenshot of Intellisense drop-down list
Screen shot of intellisense Pop-up drop-down menu. Menu pops up after typing “.” after a valid object name or “ctrl+J”

Intellisense pops up immediately when one types a period following the name of an existing object. But, there’s a catch: if the object is defined with a Dim statement using “As Object” instead of as a specific type, it is not available.

Also, intellisense is available to add commands and objects by pressing ctrl+J.

The other advantage to early binding comes when a program is run. Early binding decreases the time it takes to set an object. Not a big deal for small macros, but it could add up for larger program.

Leave a Reply

You must be logged in to post a comment.