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

Understanding the MapBasic Main procedure  

30/10/2016

0 Comments

 
If you look up the Main procedure in MapBasic Help you will see that it is defined as the first procedure called when MapBasic is run. In the last blog, A first look at MapBasic, a Note statement was compiled and run without using this Main procedure. In that case MapBasic was run in an implicit mode. The program assumed that you needed to also run the Main procedure and therefore automatically included it. If you want to run more complex programs you will need to explicitly use the Main procedure. A more detailed explanation of implicit and explicit requirements are given within MapBasic Help so I will not repeat it here. Also I would encourage you to use the help facility to build on your understanding of MapBasic.

Before you can use procedures such as Main within your MapBasic program you must first declare them. As show in the code below:-
Picture
You could save this example as a simple template for future programs. This saves having to repeatedly type the same code and also avoids potential syntax errors.

The Main procedure controls all the other sub procedures to ensure the program runs according to the programmer’s intention. For any procedure to work in must first be declared. The Declare function defines the name and parameter list of a function. Search for the Declare function in MapBasic Help and scroll down to the example section. Here you will find a program for calculating cubes which may help improve your understanding of some basic MapBasic programming concepts

This example below is a more simplified version which covers a number of features worthy of further discussion.
Picture
The Declare function statement is necessary for MapBasic as it defines the name and any parameters of a function. You put the Declare statements at the top of your program and this will always include at least the Declare Sub Main function statement.

At this point you will also have to learn a little about variables. In this program we have also declared two variables x and Cube both are of type SmallInt.  SmallInt variable types are whole numbers from -32768 to 32767 (inclusive); stored in 2 bytes.  Any variable information must be declared before being used in the program. This enables MapBasic, in this example, to allocate memory to accommodate SmallInt integers associated with the two variables.  For more information about variables search for the Dim statement in MapBasic Help.

The result of the equation x ^ 3 is saved in the variable Cube. The caret “^” signifies that x is raised by the power of 3 in this case.

When the program is compiled and run the output is displayed in MapInfo Professional in a Note statement dialogue box. When the OK button is clicked the box is removed and the program finishes.
Picture
The following code  Note(“The cube of 3 is “ Str$(Cube)) enables MapBasic to display the Note message correctly within the Note dialogue box. Any text needs to be included in double quotes. An extra space is also included to ensure the variable is printed out correctly. The Str$()  function enables MapBasic to convert the variable Cube, which is an integer, to a string format so that it displays correctly.

Whilst this program only contains a few lines of code there is still a lot to understand if you are completely new to programming. Like with learning any language there are some basic concepts you have to understand before you can successfully create more meaningful programs.
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