Type (statement)

Syntax

Type username
  
variable As type
  variable
As type
  variable
As type
  
:
End Type

Description

The Type statement creates a structure definition that can then be used with the Dim statement to declare variables of that type. The username field specifies the name of the structure that is used later with the Dim statement.

Comments

Within a structure definition appear field descriptions in the format:

  variable As type

where variable is the name of a field of the structure, and type is the data type for that variable. Any fundamental data type or previously declared user-defined data type can be used within the structure definition (structures within structures are allowed). Only fixed arrays can appear within structure definitions.

The Type statement can only appear outside of subroutine and function declarations.

 

When declaring strings within fixed-size types, it is useful to declare the strings as fixed-length. Fixed-length strings are stored within the structure itself rather than in the string space. For example, the following structure will always require 62 bytes of storage:

  Type Person
    FirstName As String * 20
    LastName As String * 40
    Age As Integer
  End Type

Note: Fixed-length strings within structures are size-adjusted upward to an even byte boundary. Thus, a fixed-length string of length 5 will occupy 6 bytes of storage within the structure.

Example

This example displays the use of the Type statement to create a structure representing the parts of a circle and assign values to them.

Type Circ
  msg As String
  rad As Integer
  dia As Integer
  are As Double
  cir As Double
End Type

Sub Main()
  Dim circle As Circ
  circle.rad = 5
  circle.dia = circle.rad * 2
  circle.are = (circle.rad ^ 2) * Pi
  circle.cir = circle.dia * Pi
  circle.msg = "The area of this circle is: " & circle.are
  MsgBox circle.msg
End Sub

See Also

Dim (statement); Public (statement); Private (statement).

More information

T