Stepping Through Code

Code in a subroutine or function can be stepped through during execution to help identify problems and evaluate branching within the code.

The execution of a subroutine or function can be paused at its declaration line, or by placing a breakpoint on the line of code that execution should be paused. Once execution has been paused, you can then step through and evaluate the results of each line of code one at a time. Normal execution can be restored when you are done evaluating the lines of code.

In the following steps, you learn to step through the gardenpath macro with the Step Into feature.

  1. In AutoCAD, click Manage tab Applications panel Run VBA Macro. Find
  2. In the Macros dialog box, select the ThisDrawing.gardenpath macro and click Step Into.

    The VBA IDE is opened and execution of the gardenpath macro is paused. You will know it is paused because the declaration line Public Sub gardenpath() is highlighted.

  3. On the menu bar, click Debug Step Into to step to the next code statement.

    The code statement gpuser is now highlighted.

    Note: You can also click Step Into on the Debug toolbar.
  4. Press F8 to step into the gpuser subroutine.

    Execution moves to the declaration line of the gpuser subroutine. The code statement Private Sub gpuser() is highlighted.

  5. Press F8 twice to execute the GetPoint method.

    Notice that the highlight disappears and no new code is presented. This is because the GetPoint method is waiting for the user to enter a point in AutoCAD.

  6. Switch back to the AutoCAD window.

    You see the prompt you specified in the GetPoint method at the Command prompt.

  7. At the Specify start point of path: prompt, specify a point in the drawing area.

    Focus is returned to the VBA IDE and the code statement sp(0) = varRet(0) is highlighted.

  8. In the code editor window, position the cursor over varRet(0). Notice a tooltip appears with a value in it of which is the X coordinate value of the point specified in the drawing. This tooltip contains the current value assigned to the first element of the array stored in the varRet variable.
  9. On the menu bar, click View Locals Window.

    The Locals window is displayed. All the local variables and their values are displayed in the Locals window while the macro is executing.

  10. In the Locals window, expand the varRet item and evaluate the double values assigned to the array.
  11. On the menu bar, click Run Continue to resume normal execution. Switch back to AutoCAD and specify values for each of the remaining prompts.