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

Creating a simple MapBasic program

20/11/2016

0 Comments

 
One way to effectively learn how to code is to look at code examples and try to understand what the program is doing. Additionally, you also need to write the code into the MapBasic Development Environment to fully grasp what is going on in the code sample, rather than just reading or cutting and pasting.

Lets start by creating a simple code example which covers the Print, Note and Loop facilities within MapBasic.  Make a new file in MapBasic using File – New then save this Untitled file as SimpleCodeExample. This will create a new file called SimpleCodeExample.mb. The .mb extension on the file shows that it has not been compiled and can be used to input MapBasic code to create a new program.
Type the following into the MapBasic Development Environment:-

' Program: SimpleCodeExample
​
' Purpose: Show the note, print and loop facilities
​
By putting a single quotation mark in front of these two lines we have indicated to MapBasic that we want this to be for information only purposes. So the compiler will ignore these lines when we create an executable file.
​

Next we will put in a Declare statement which MapBasic Help states ‘declares the name and parameter list of a function’. I would recommend that you type ‘declare’ into the search facility of MapBasic Help for a full description of the Declare Function statement. In effect you have to tell MapBasic exactly what you are going to do at every stage. An example of this is the use of the Declare statement.
Type the following into the MapBasic Development Environment after the previous code input:-

​
' Declare

Declare Sub 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

Dim 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 Main function has been likened to a telephone exchange as it holds all the procedures and functions used in the program. In this simple example we also used the Dim statement to create an integer variable. Variables can hold a range of data types including characters, strings, numbers etc. At this stage you could equate a variable to a container which holds some kind of data. Use the MapBasic Help to look up more information about the Main procedure, the Dim statement and variable types.


The For … Next statement enables you to loop a specific number of times. For more information search for looping – For … Next statement in the MapBasic Help.

Within the loop statement we have to convert the integer variable “i” to a string so the Print statement can correctly print it out. This is achieved with the Str$() function. A full description of this function is again available within MapBasic Help.

Finally the End Sub ends the program.

Below is the code sample as it appears within the MapBasic Development Environment:-
Picture
When this code is compiled and executed MapInfo Pro should look like the following image:-
​
​
Picture

​The main difference between the Note and Print statements is that the Note statement appears in a dialogue box and halts the program until the OK button is pressed. The Print statements appear in the Message box and do not halt execution of the program. As well as communicating with the user, the Note and Print statements are very useful in correcting coding errors.


The various features of this coding sample are useful when beginning to learn how to program with MapBasic and could be helpful when progressing to more complex programs involving mapping functionality.
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

    March 2024
    October 2023
    September 2023
    August 2023
    March 2023
    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