The ArcPad Team Blog

Unofficial stuff from the team behind the World's leading mobile GIS platform

Tuesday, August 22, 2006

Debugging Tips # 1 - Using the ArcPad 7 Console Window

A common practice in debugging when customising ArcPad with scripts is to place message boxes in your script code to inspect variables or check that your scripts are executing via a particular logic path. However using message boxes can introduce problems as they interrupt the execution of your code and can affect the way your application runs. Message boxes are also not practical for debugging background activity such as GPS and communications events (eg OnPosition, OnComm, etc.) . Message boxes can also be annoying if they appear frequently and usually cannot be easily left in your application for future debugging or in deployment code.

ArcPad 7 introduced the Console window object which can be used as an alternative way of displaying messages without the side effect of interrupted application flow. The Console window can also be used for background activity messages.

Using the Console window is very simple. For example you might be using something like the following VBScript code to display the contents of a variable named Value:

MsgBox “The value is” & Value

To display Value in the Console window instead, use:

Console.Print “The value is” & Value

Or in JScript :

Console.Print("The value is " + Value);

When the Console.Print method is run, the ArcPad console window will be automatically opened and the message displayed without interrupting the application flow.

You can also use color to improve the display of your messages, for example :

Console.Print("ArcPad 7 Is Very Cool”, apRed, apYellow);

Would display red text on a yellow background.

The contents of the Console can be cleared at any time using Console.Clear method, or hidden using Console.Visible = False. You can also change the title of the Console window by setting the Console.Caption property and the background colour of the window via Console.Color. Eg:

Console.Caption = “Debug Messages”
Console.Color = apCyan

You may also wish to keep your debug messages in your application and allow the user to enable them when support is required. Simply wrap each Console.Print call with an if statement which checks if the application is running in a debug

If Application.UserProperties(“debug”) = true then
Console.Print “The value is” & Value
End If

In the example above the debug state is kept in the Application.UserProperties(“debug”) variable. This approach uses an ArcPad application global variable that is accessible across all applets, forms and layers. This then allows debug messages to be turned on or off via a single simple statement. You can also leave all your handy debug messages in your code, without impacting the user. Eg :

Application.UserProperties(“debug”) = true

Attach script code similar to the above to an ArcPad toolbar button, and your application user can then easily turn on or off debug messages via just a toolbar button click. This technique could be useful if you have to provide support to remote users and you’d like to them to report some of the debug messages you left in your code.

Another useful feature of the Console object is that it can be used to display ArcPad’s internal debugging messages, which is especially useful for watching which events are firing when ArcPad is running and seeing map draw timing information. To enable the internal debug messages, set the Console.DebugFlags property to 3. Eg:

Console.DebugFlags = 3

Finally, the Console window is not limited to just debugging code, you can also consider it as another way of presenting messages in your application such as status information, when user intervention is not required.