Condividi tramite


Visual Basic Concepts

Creating Your Own Data Types

You can combine variables of several different types to create user-defined types (known as structs in the C programming language). User-defined types are useful when you want to create a single variable that records several related pieces of information.

You create a user-defined type with the Type statement, which must be placed in the Declarations section of a module. User-defined types can be declared as Private or Public with the appropriate keyword. For example:

Private Type MyDataType

-or-

Public Type MyDataType

For example, you could create a user-defined type that records information about a computer system:

' Declarations (of a standard module).
Private Type SystemInfo
   CPU As Variant
   Memory As Long
   VideoColors As Integer
   Cost As Currency
   PurchaseDate As Variant
End Type

Declaring Variables of a User-Defined Type

You can declare local, private module-level, or public module-level variables of the same user-defined type:

Dim MySystem As SystemInfo, YourSystem As SystemInfo

The following table illustrates where, and with what scope, you can declare user-defined types and their variables.


Procedure/Module
You can create a user-defined type as... Variables of a user-defined
type can be declared...
Procedures Not applicable Local only
Standard modules Private or public Private or public
Form modules Private only Private only
Class modules Private or public Private or public

Note   If declared using the Dim keyword, user-defined types in Standard or Class modules will default to Public. If you intend a user-defined type to be private, make sure you declare it using the Private keyword.

Assigning and Retrieving Values

Assigning and retrieving values from the elements of this variable is similar to setting and getting properties:

MySystem.CPU = "486"
If MySystem.PurchaseDate > #1/1/92# Then

You can also assign one variable to another if they are both of the same user-defined type. This assigns all the elements of one variable to the same elements in the other variable.

YourSystem = MySystem

User-Defined Types that Contain Arrays

A user-defined type can contain an ordinary (fixed-size) array. For example:

Type SystemInfo
   CPU As Variant
   Memory As Long
   DiskDrives(25) As String   ' Fixed-size array.
   VideoColors As Integer
   Cost As Currency
   PurchaseDate As Variant
End Type

It can also contain a dynamic array.

Type SystemInfo
   CPU As Variant
   Memory As Long
   DiskDrives() As String      ' Dynamic array.
   VideoColors As Integer
   Cost As Currency
   PurchaseDate As Variant
End Type

You can access the values in an array within a user-defined type in the same way that you access the property of an object.

Dim MySystem As SystemInfo
ReDim MySystem.DiskDrives(3)
MySystem.DiskDrives(0) = "1.44 MB"

You can also declare an array of user-defined types:

Dim AllSystems(100) As SystemInfo

Follow the same rules to access the components of this data structure.

AllSystems(5).CPU = "386SX"
AllSystems(5).DiskDrives(2) = "100M SCSI"

Passing User-Defined Types to Procedures

You can pass procedure arguments using a user-defined type.

Sub FillSystem (SomeSystem As SystemInfo)
   SomeSystem.CPU = lstCPU.Text
   SomeSystem.Memory = txtMemory.Text
   SomeSystem.Cost = txtCost.Text
   SomeSystem.PurchaseDate = Now
End Sub

Note   If you want to pass a user-defined type in a form module, the procedure must be private.

You can return user-defined types from functions, and you can pass a user-defined type variable to a procedure as one of the arguments. User-defined types are always passed by reference, so the procedure can modify the argument and return it to the calling procedure, as illustrated in the previous example.

Note   Because user-defined types are always passed by reference, all of the data contained in the user-defined type will be passed to and returned from the procedure. For user-defined types that contain large arrays, this could result in poor performance, especially in client/server applications where a procedure may be running on a remote machine. In such a situation, it is better to extract and pass only the necessary data from the user-defined type.

For More Information   To read more about passing by reference, see "Passing Arguments to Procedures" in "Programming Fundamentals."

User-Defined Types that Contain Objects

User-defined types can also contain objects.

Private Type AccountPack
   frmInput as Form
   dbPayRollAccount as Database
End Type

Tip   Because the Variant data type can store many different types of data, a Variant array can be used in many situations where you might expect to use a user-defined type. A Variant array is actually more flexible than a user-defined type, because you can change the type of data you store in each element at any time, and you can make the array dynamic so that you can change its size as necessary. However, a Variant array always uses more memory than an equivalent user-defined type.

Nesting Data Structures

Nesting data structures can get as complex as you like. In fact, user-defined types can contain other user-defined types, as shown in the following example. To make your code more readable and easier to debug, try to keep all the code that defines user-defined data types in one module.

Type DriveInfo
   Type As String
   Size As Long
End Type

Type SystemInfo
   CPU As Variant
   Memory As Long
   DiskDrives(26) As DriveInfo
   Cost As Currency
   PurchaseDate As Variant
End Type

Dim AllSystems(100) As SystemInfo
AllSystems(1).DiskDrives(0).Type = "Floppy"