You have for a long time been able to use .NET method from MapBasic. This gives you access to a very powerful world of new capabilities when developing solutions for MapInfo Pro.
There are a number of things that are easier to do in .NET than in MapBasic.
In order to use a .NET method developed with one of the .NET development languages such as C# and VB.NET, you need to make sure that
- your method is declared as a static method,
- the number and type of parameters to match
- the name space and class name match
In MapBasic you can use the Declare Method to describe the method to MapBasic. Here is a description of the Declare Method statement from the MapBasic Help File (see the Help File for full description).
Syntax
Declare Method fname
Class "class_name"
Lib "assembly_name" [ Alias function_alias ]
( [ [ ByVal ] parameter [ , parameter... ] As var_type ]
[ , [ ByVal ] parameter [ , parameter... ] As var_type... ]
) [ As return_type ]
- fname is the name by which a function will be called; if the optional Alias clause is omitted, fname must be the same as the actual .NET method/function name. This option can not be longer than 31 characters.
- class_name is the name of the .NET class that provides the function to be called, including the class's namespace (such as System.Windows.Forms.MessageBox)
- assembly_name is the name of a .NET assembly file, such as filename.dll. If the assembly is to be loaded from the GAC, assembly_name must be a fully qualified assembly name.
- function_alias is the original name of the .NET method/function (the name as defined in the .NET assembly). Note: Include the Alias clause only when you want to call the method by a name other than its original name.
- parameter is the name of a parameter to the function.
- var_type is a MapBasic data type, such as Integer
- return_type is a standard MapBasic scalar variable type, such as Integer. If the As clause is omitted, the MapBasic program can call the method as a Sub (using the Call statement).
Description
The Declare Method statement allows a MapBasic program to call a method (function or procedure) from a .NET assembly. The .NET assembly can be created using various languages, such as C# or VB.NET. For details on calling .NET from MapBasic, see the MapBasic User Guide.
MapBasic programs can only call .NET methods or functions that are declared as static. (VB.NET refers to such functions as "shared functions," while C# refers to them as "static methods."). This limitation has however been removed with the introduction of the 64 bit version of MapInfo Pro.
Examples
Here is a simple example of a C# class that provides a static method:
namespace MyProduct
{
class MyWrapper
{
public static int ShowMessage(string s)
{
System.Windows.Forms.MessageBox.Show(s);
return 0;
}
}
}
In VB.NET, the class definition might look like this.
Namespace MyProduct
Public Class MyWrapper
Public Shared Function ShowMessage(ByVal s As String) As Integer
System.Windows.Forms.MessageBox.Show(s)
Return 0
End Function
End Class
End Namespace
A MapBasic program could call the method with this syntax:
Declare Method ShowMessage Class "MyProduct.MyWrapper" Lib "MyAssembly.DLL" (ByVal str As String) As Integer
. . .
Dim retval As Integer
retval = ShowMessage("Here I am")
------------------------------
Peter Horsbøll Møller
Pitney Bowes
------------------------------