User Defined Types

A user defined type (or data structure) lets you create a single data type that can represent more than one built-in data type.
Often abbreviated to UDTs.
The default value is the default value of the individual elements
You create a user defined type using the Type statement.
A type is a collection of variables (or fields)
This combines multiple data types into a single data type.


Type type_UserDefined 
   sField1 As String
   iField2 As Integer
   bField3 As Boolean
End Type

This allows you to define a single variable to access all the fields
You define custom data types outside of procedures at the top of your module. Once you have created your type use the Dim statement to declare a variable of that type.

Dim uMyUserDefinedType As type_UserDefined 
uMyUserDefinedType.Field1 = "Tuesday"
uMyUserDefinedType.Field2 = 24
uMyUserDefinedType.Field3 = False

Types can be very useful but they do have a number of limitations:
You can't declare a new instance of a type. They must all be declared at design time
You can't validate or restrict the values that are assigned to the different fields
A type is just a static data structure and cannot have any actions associated with it
Using Classes instead of types can overcome all these limitations
You define user defined data types outside at the very top of your modules before any procedures or functions.
UDTs are far more efficient than using a Collection object
It is important to visually distinguish user defined types from other data types.
For this reason is should always be prefixed with an uppercase T.

Type type_ContactInfo 
   lContactNo As Long
End Type

Type type_Person 
   sName As String
   sAge As String
   lDateBorn As Long
End Type

Once you have declared the user defined type you can use the Dim statement to declare a variable of that type.

Dim uMyVariable As TPerson 

Array

A fixed array is an example of a static data structure where you know the total number of elements
Arrays are linear
Arrays can be resized but not very efficiently.
Arrays can be sorted but not very efficiently.
Arrays do not use space very efficiently
If you declare an array to hold 50 elements and then only use 5.
More Details


Dynamic Data Types

These always refer to data in memory.
The size only increases when necessary
Contains at least once to another instance of the same class
The simplest example is a linear data structure


Linear Data Types

This is a dynamic data structure
Each element contains information and a reference to another element of the same type
Easy to add and remove elements in any position
Easy to resize
Typically include at least one header item


VB 6.0

Since VB 6.0 user defined types can be passed as property values.
Since VB 6.0 user defined type can be used in public function declarations


Important

When defining a user defined type all variables within the type should be in capitals
A UDT is not a variable and does not take up any space.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext