jps services
  • Home Page
  • GIS Training Courses
    • QGIS Training Courses
    • ArcGIS Training Courses
    • MapInfo Pro Training Courses >
      • MapBasic Training Course
  • About
  • Blog

Fixing MapBasic coding errors

28/11/2016

0 Comments

 
No matter how carefully you code you will from time to time find that your MapBasic code will not compile. When this happens you will have one or more messages appear at the bottom of the MapBasic Development Environment window. Some of these messages are self explanatory. For example, you have failed to initialise a variable or have not included the necessary definition files. You also need to be aware that, even though your code may compile, because of logic errors it still may not perform in the way you intended. In the previous blog Creating a simple MapBasic program the code had no errors and would have compiled and run as intended.

Below is code but containing a few errors. If we try to compile this code it will generate a number of error messages.
On this occasion I would suggest you copy and paste this code into MapBasic if you are not familiar with correcting coding errors. Normally I would always recommend typing coding samples, as you learn more through that method. However, on this occasion copying and pasting is the correct procedure.
Program: SimpleCodeExample with errors
Purpose: Show the note, print and loop facilities
' Declare
Declare Main
' Function: Main
' Purpose: Loop five times and print out a message each time
' Shows how the print, note and loop statements work
Sub Main
i As Integer
' the loop will execute a Print statement 5 times
For i = 1 to 5
Print "Hello world! The count is now " + Str$(i)
Next
' Let the user know that looping has finished
Note The loop is now finished."
End Sub​
The above code will generate errors and display an error status message box, if you attempt to compile it, as shown in the following image:-​
Picture
If you click OK on this message box a number of errors will appear at the bottom of the MapBasic window similar to the image below:-​

Picture
In fact we have generated so many errors that they do not all show. There are in fact ten error messages. There are a number of ways of dealing with these error messages. You could try to resolve each one and then try to compile your code again. I personally prefer to take each error in turn and try to resolve it. Then compile again to see how many errors are still displaying. However, you will have to work out which method works best for you.

As you can see the first message states that on line four MapBasic has found an unrecognised command Show. If you double click on the error message it will take you to the relevant line. Alternatively you can use Search – Go to line from the top menu bar or use the Ctrl+T short cut option. This will open a dialogue box in which you can specify the line number you want to go to. In this case the error was a missing single quote as the line was for information purposes only. If you compile again a dialogue box asks if you want to save the changes so click OK. Again we get the error message dialogue box. The first message now refers to line 8. Found [Main] whilst searching for [Sub]. Here the correct notation should have been Sub Main. Correcting this error and recompiling shows that we have four error messages to deal with. This has happened even though we have corrected only two errors against the original ten. Fixing those two errors reduced the number of errors found significantly as seen in the screen shot below:​
Picture

T
he error on line 18 is caused by the lack of a Dim statement. Fixing this line reduces the errors from four to two. The error at line 30 is caused by a missing double quotation mark in the Note statement. When you fix this error the final error also disappears and the code is successfully compiled.
​

The code below highlights the lines containing errors together with an explanation of why this causes a compile problem.
' Program: SimpleCodeExample
Purpose: Show the note, print and loop facilities [Missing single quote]
' Declare
Declare Main [Missing Sub]
' Function: Main
' Purpose: Loop five times and print out a message each time
' Shows how the print, note and loop statements work
Sub Main
i As Integer [Dim statement missing]
' the loop will execute a Print statement 5 times
For i = 1 to 5
Print "Hello world! The count is now " + Str$(i)
Next
' Let the user know that looping has finished
Note The loop is now finished." [Missing opening double quote ]
End Sub​
As previously mentioned, although the above code initially generated ten errors there were only four errors in the code. So even though you may find many errors being generated at first you may find that you only have a handful of errors to correct to clear many MapBasic error messages.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Author

    Joe Short BSc has been involved with various mapping solutions for over twenty years.  If you are considering implementing a GIS  or have ArcGIS Pro, MapInfo Pro or QGIS training requirements, jps services would be happy to be of assistance to your organisation. 

    Archives

    April 2020
    March 2020
    October 2019
    September 2019
    August 2019
    July 2019
    March 2019
    November 2018
    October 2018
    August 2018
    July 2018
    November 2017
    October 2017
    September 2017
    July 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    May 2016
    February 2016
    September 2015
    August 2015
    April 2015
    February 2015
    November 2014
    October 2014
    July 2014
    June 2014
    May 2014
    March 2014
    February 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    June 2013
    May 2013
    April 2013
    February 2013
    December 2012
    October 2012
    September 2012
    August 2012
    July 2012

    Categories

    All
    Arc Gis
    Arcview
    Autodesk
    Cad
    Gis Training
    Local Government
    Mapbasic
    Mapinfo
    Quantum Gis
    Relational Databases
    Saga Gis

    RSS Feed