Navigation:  ThinBASIC Language > Data types and variables >

UDT (User Defined Types)

Previous pageReturn to chapter overviewNext page

 

Description

 

ThinBASIC offers many numeric and string data types. These are very useful in number crunching and text manipulation.

Once you start describing real world objects or more complex math problems, you may feel the need to somehow group the data together into more complex structures.

These structures are called user defined types (UDT) in thinBasic.

 

Syntax

 

ThinBASIC uses type / end type block to denote user defined type definition.

 

TYPE nameOfType [BYTE | WORD | DWORD] [EXTENDS baseType]

  element

  [otherElements]

  [dataHandlingfunctions]

END TYPE

 

TYPE MyType [BYTE | WORD | DWORD]

[STATIC] [MemberName AS TypeName]

[STATIC] [MemberArrayName[(nElements)] AS TypeName]

[...]

END TYPE

 

Returns

 

None

 

Member alignment

 

TYPE definitions may optionally specify an alignment of BYTE (the default), WORD, DWORD

BYTE: each member will be aligned on a BYTE boundary - no padding or alignment is applied to the structure. This is the default alignment method.
WORD: each member will be aligned on a WORD boundary. Any odd byte between members of TYPE will be automatically skipped and ignored.
The UDT structure is also padded with one trailing byte to ensure the total structure size is a multiple of 2 bytes.
DWORD: each member will be aligned on a DWORD boundary. Up to three bytes will be skipped to accomplish this alignment.
The UDT structure is also padded with up to three trailing bytes to ensure the total structure size is a multiple of 4 bytes.

 

Remarks

 

thinBasic support any level of nesting types.

 

Elements inside a type can be:

numeric (any supported type)
string (fixed or dynamic strings)
variants
another type (allowing nesting types inside another one)
union

 

To indicate a fixed string as UDT element use the following notation:

'...

DynStr As String * StringSizeInBytes

'...

 

To indicate a dynamic string as UDT element use the following notation:

'...

DynStr As String

'...

 

A dynamic string is represented inside an UDT as 32bit number so it will occupy only 4 bytes regardless of its size.

thinBasic will take care of memory allocation and de-allocation of dynamic strings.

 

A VARIANT element will always occupy 16 bytes.

 

STATIC elements

static elements are elements in common to all UDT variables derived from the same UDT TYPE.

They are not part of the UDT structure itself but are available to all UDT of the same type.

Static variables retain their values as long as the program is running.

 

Restrictions

 

See also

 

Dim

 

Examples

 

'---

 

 

Description

 

 

 

Syntax

 

 

Syntax

ThinBASIC uses type / end type block to denote user defined type definition.

 

TYPE nameOfType [alignmentModifier] [EXTENDS baseType]

 element

 [otherElements]

 [dataHandlingfunctions]

END TYPE

 

Example

The definition of type looks complicated, but keep in mind only two parameters are mandatory:

 

name of type

at least one element

To give specific example, if you want to represent a point in 2D, you could design the following UDT:

 

TYPE Point2D

 x AS SINGLE

 y AS SINGLE

END TYPE

The name of type is Point2D and it has two elements - x and y, each of SINGLE basic data type.

 

You can create variable of user defined type the same way as you do with conventional data types:

 

DIM point AS Point2D

In order to write or read the data, you can use the dot notation:

 

point.x = 1

point.y = 2

 

msgBox 0, strFormat$("{1}, {2}", point.x, point.y)

Arrays of user defined types are supported as well.