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.
' 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.
' 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:-
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.