MapInfo Pro

 View Only
  • 1.  MapBasic Monday: Creating a simple MapBasic program

    Employee
    Posted yesterday

    This week's #MapInfoMonday article has been written by Joe Short.

    To effectively learn how to code, you need 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 cutting and pasting. 

    Let's start by creating a simple code example, which covers the Print, Note, and Loop facilities within MapBasic. Start by making 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:-

    ' 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 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 out the count integer variable. 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. 

    The main difference between the Note and Print statements are that the Note statement appears in a dialogue box and halts the program until the OK button is pressed. The Print statements appear in a 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 errors in code.

    The various features of this coding sample are useful in learning how to program with MapBasic and will be of use when progressing to more complex programs involving mapping functionality.

    Below is the code sample within the MapBasic Development Environment:

    image
    When this code is compiled and run, MapInfo Pro should look like the following image:
    image
    Thanks for sharing this article, Joe.
    If you are looking for more MapBasic samples, look at the Writing MapBasic Applications section on the Home of #MapInfoMonday page.


    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------


  • 2.  RE: MapBasic Monday: Creating a simple MapBasic program

    Posted 5 hours ago
    Edited by Uffe Kousgaard 5 hours ago

    Here is another small example, which I wrote a couple of days ago. I had an access database with many very wide char fields in it. After saving it as a normal TAB file, I wanted to trim the char fields, so they were not all 254 characters wide. Main feature is dynamically building an ALTER TABLE statement in code (s2 variable) and finally running it through the Run Command statement.

    ' This application will shorten all char fields in a TAB
    ' to the maximum required length, according to the content
    
    include "mapbasic.def"
    
    open table "d:\somepath\sometable" as inputtable
    
    dim i,N as integer
    dim s1,s2,fieldname as string
    dim firstField,CharFieldFound as logical
    
    s2 = "alter table inputtable (modify "
    firstField = true
    CharFieldFound = false
    for i = 1 to tableinfo(inputtable,TAB_INFO_NCOLS)
      if columninfo(inputtable,"COL"+str$(i),COL_INFO_TYPE) = COL_TYPE_CHAR then
        fieldname = columninfo(inputtable,"COL"+str$(i),COL_INFO_NAME)
    
        ' find maximum length of char field
        s1 = "select max(len(" + fieldname + ")) from inputtable into tmp1"
        run command s1
        fetch first from tmp1
        N = tmp1.col1
    
        if firstField = false then 
          s2 = s2 + ","
        end if
        s2 = s2 + " " + fieldname + " Char(" + str$(N) + ")"
        firstField = false
        CharFieldFound = true
      end if
    next
    s2 = s2 + ")"
    if CharFieldFound = true then
      run command s2
    end if
    



    ------------------------------
    Uffe Kousgaard
    CEO
    Routeware v/Uffe Kousgaard
    Roskilde
    ------------------------------