User Defined Data Types

Variables of different data types when combined as a single variable to hold several related informations is called a User-Defined data type.

A Type statement is used to define a user-defined type in the General declaration section of a form or module. User-defined data types can only be private in form while in standard modules can be public or private. An example for a user defined data type to hold the product details is as given below.

Private Type ProductDetails

ProdID as String

ProdName as String

Price as Currency

End Type

The user defined data type can be declared with a variable using the Dim statement as in any other variable declaration statement. An array of these user-defined data types can also be declared. An example to consolidate these two features is given below.

Dim ElectronicGoods as ProductDetails ' One Record

Dim ElectronicGoods(10) as ProductDetails ' An array of 11 records

A User-Defined data type can be referenced in an application by using the variable name in the procedure along with the item name in the Type block

. Say, for example if the text property of a TextBox namely text1 is to be assigned the name of the electronic good, the statement can be written as given below.

Text1.Text = ElectronicGoods.ProdName

If the same is implemented as an array, then the statement becomes

Text1.Text = ElectronicGoods(i).ProdName

User-defined data types can also be passed to procedures to allow many related items as one argument.

Sub ProdData( ElectronicGoods as ProductDetails)

Text1.Text = ElectronicGoods.ProdName

Text1.Text = ElectronicGoods.Price

End Sub

PREVIOUS VB CONTENTS HOME NEXT