Purpose
The first procedure called when an application is run.
Syntax
Declare Sub Main
Sub Main
statement_list
End Sub
statement_list is a list of statements to execute when an application is run.
Description
Main is a special-purpose MapBasic procedure name. If an application contains a sub procedure called Main, MapInfo Pro runs that procedure automatically when the application is first run. The Main procedure can then take actions (for example, issuing Call statements) to cause other sub procedures to be executed.
However, you are not required to explicitly declare the Main procedure. Instead of declaring a procedure named Main, you can simply place one or more statements at or near the top of your program file, outside of any procedure declaration. MapBasic will then treat that group of statements as if they were in a Main procedure. This is known as an "implicit" Main procedure (as opposed to an "explicit" Main procedure).
Example
A MapBasic program can be as short as a single line. For example, you could create a MapBasic program consisting only of the following statement:
Note "Testing, one two three."
If the statement above comprises your entire program, MapBasic considers that program to be in an implicit Main procedure. When you run that application, MapBasic will execute the Note statement.
Alternately, the following example explicitly declares the Main procedure, producing the same results (for example, a Note statement).
Declare Sub Main
Sub Main
Note "Testing, one two three."
End Sub
The next example contains an implicit Main procedure, and a separate sub procedure called Talk. The implicit Main procedure calls the Talk procedure through the Call statement.
Declare Sub Talk(ByVal msg As String)
Call Talk("Hello")
Call Talk("Goodbye")
Sub Talk(ByVal msg As String)
Note msg
End Sub
The next example contains an explicit Main procedure, and a separate sub procedure called Talk. The Main procedure calls the Talk procedure through the Call statement.
Declare Sub Main
Declare Sub Talk(ByVal msg As String)
Sub Main
Call Talk("Hello")
Call Talk("Goodbye")
End Sub
Sub Talk(ByVal msg As String)
Note msg
End Sub
See Also:
EndHandler procedure, RemoteMsgHandler procedure, SelChangedHandler procedure, Sub...End Sub statement, ToolHandler procedure, WinClosedHandler procedure