TYPE (or UDT User Defined Types)

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Core Language > Data types and variables >

TYPE (or UDT User Defined Types)

 

Description

 

Define a User-Defined Data Type (UDT), containing one or more member elements.

 

Syntax

 

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: BYTE, INTEGER, LONG, QUAD, WORD, DWORD, SINGLE, DOUBLE, EXTENDED and CURRENCY

string: STRING, ASCIIZ

hybrid: VARIANT

another used defined type (allowing nesting types inside another one) or 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

 

'---

Type MyRect

  x As Long

  y As Long

  w As Long

  h As Long

End Type

 

'---

Type MyData

  Val1 As Integer

  Val2 As Long

  Val3 As String * 255

  dynStr As String PTR  '---Dynamic string

  Val4 As EXT

  Re   As MyRect      '---Note, a type inside a type

  Val5 As QUAD

  Val7 As Double

End Type

 

Dim Info As MyData

 

Info.Val1 = 10

Info.Re.x = 20

 

MSGBOX 0, Info.Val1 & "-" & Info.Re.x

 

 

'----Example of a type inside another type:

Type Vector2d

  X as Double

  Y as Double

End Type

 

Type Vector3d

  Vector2d

  Z as Double

End Type

 

dim foo as Vector3d

 

foo.X = 1.23

foo.Y = 2.34

foo.Z = 3.45