Skip to content

Conditional Breakpoints

You can set breakpoints in your Xojo code that cause the debugger to appear when the line of code with the breakpoint is reached. This is incredibly handy to help understand and test your code.

One thing that people often want is the ability to have a breakpoint only get used if a specific condition occurs. This is called a “conditional breakpoint”. Here are a couple¬†ways you can set a conditional breakpoint, both of which require slight tweaks to your code.

Break

The Xojo Programming Language command “Break” immediately makes the debugger appear with the code stopped on that line. It has no effect for apps built for deployment. You can use this command with simple conditional statements to trigger a breakpoint for specific situations.

For example, if you are processing a file with 800 lines of data in a loop, but you’ve discovered a bug near the end of the data processing in line 700. What would be the best way to debug this? Well, you certainly don’t want to set a standard breakpoint inside the loop because this will cause the debugger to appear 700 times and force you to press resume 700 times. That would be painful.

Instead, you can simply check the line count and then use the Break command:

Dim line As Integer
While Not inputStream.EOF
  line = line + 1
  Dim lineText As Text = myFile.ReadLine
  If line = 700 Then Break
  ' ... processing here
Wend

Now the Debugger only appears when line 700 is reached and does not appear for any other line.

Computed Property

There may be times when you want to be notified any time a property value changes. The trick for this is to create the property as a Computed Property and use the Break command within the Set section.

ComputedBreak.png

Any time the property is assigned a value, the code in the Set section is called, which drops you into the debugger so you can check the state of your app.

I hope these two tips help you get more out of the Xojo Debugger. For more information, refer to Using the Debugger in the User Guide or the Debugging video.


XDC 2016 Houston, Texas