Type statement - MapBasic - 2023

MapInfo MapBasic Reference

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapBasic
Version
2023
Language
English
Product name
MapBasic
Title
MapInfo MapBasic Reference
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:32:32.686312

Purpose

Defines a custom variable type which can be used in later Dim statements and Global statements.

Syntax

Type type_name Align[n]
	element_name As var_type
	[ ... ] 
End Type

type_name is the name you define for the data type.

Align[n] optionally defines the struct packing alignment for the target DLL. Valid values are [1|2|4|8|16].

element_name is the name you define for each element of the type.

var_type is the data type of that element.

Restrictions

Any Type statements must appear at the "global" level in a program file (for example, outside of any sub procedure). You cannot issue a Type statement through the MapBasic window. You cannot pass a Type variable as a by-value parameter to a procedure or function. You cannot write a Type variable to a file using a Put statement.

Description

The Type statement creates a new data type composed of elements of existing data types. You can address each element of a variable of a custom type using an expression structured as variable_name.element_name. A Type can contain elements of other custom types and elements which are arrays. You can also declare arrays of variables of a custom Type. You cannot copy the entire contents of a Type variable to another Type variable using an assignment of the form var_name = var_name.

You can pass a custom variable type as a parameter to a DLL. An optional keyword, Align[n] can be added to the Type statement which specifies the struct packing alignment for the target DLL. The keyword is analogous to the MSVC /Zp[n] compiler option for struct member alignment, or the Microsoft specific align(#) modifier for defining a struct. This option is introduced to address multiple struct alignment problems in x64, especially with strings.

If the Type statement is defined with the optional Align[n] keyword, MapInfo Pro will align the members on the n-byte boundary, in order to match the packing specification of the target DLL. If Align[n] is not specified, MapInfo Pro will use a default struct alignment that is aligned to the default packing for Visual Studio: 8 bytes for 64-bit platform or 4 bytes for 32-bit platform. You do not need to build custom DLLs with a one-byte alignment (/Zp1). This change better supports the DLLs built on x64 platforms as well as the default Windows API calls, while still accommodating user specific struct packing.

Struct alignment issues manifest by passing corrupt struct data members into or out of the DLL call. MapInfo Pro now assumes a default alignment value that matches the Visual Studio default, so to successfully integrate a MapBasic program with a custom DLL, you should either:

  • modify the MapBasic struct declaration to match the alignment value to the DLL (such as Align1); or
  • rebuild the DLL using the default struct member alignment (/Zp).

Example

Type Person Align8
	fullname As String
	age As Integer
	dateofbirth As Date
End Type 

Dim sales_mgr, sales_people(10) As Person

sales_mgr.fullname = "Otto Carto" 
sales_people(1).fullname = "Melinda Robertson"

See Also:

Dim statement, Global statement, ReDim statement