User-defined Data Types • VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables – sometimes referred to as aggregates – under one name. • UDTs may contain variables of many different data types – in contrast to arrays that contain only elements of the same data-type. • UDTs are commonly used to define fixed- length records to be stored in random- access files.
33
Embed
User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
User-defined Data Types
• VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables – sometimes referred to as aggregates – under one name.
• UDTs may contain variables of many different data types – in contrast to arrays that contain only elements of the same data-type.
• UDTs are commonly used to define fixed-length records to be stored in random-access files.
User-Defined Data Types
Integer, String, Currency, etc…….. are what are known as VB data types
You can define your own data types by combining multiple fields of data into a single unit; A User-Defined Data Type can be used
to combine several fields of related information
For example, a Product Data Type might contain a Product Description, a Product Number, Quantity, and Price
The fields can be combined into a User-Defined Data Type by using the Type and End Type Statements
User-defined Data Types
• UDTs are derived data types – they are constructed using objects of other types
• Consider the following UDT definition:Private Type ClientRecord
intAccountNumber As Integer
strLastName As String * 15
strFirstName As String * 15 ‘(fixed-length strings)
curBalance As Currency
End Type
UDT
• Keyword Type introduces the UDT definition
• Identifier ClientRecord is the Type Name
• Type definitions must be Private when declared in Form Modules, but can be declared as Public or Private in standard
Type NameOfNewDataTypeList of Fields
End Type
Type ProductstDescription As StringstProductNumber As StringiQuantity As IntegercPrice As Currency
End Type
Once you have created your own Data Type, you may use it to declare variables just as you would use any other Data Type
Type Statements can appear only at the Module Level in the General Declarations section of a Standard Code
Module or a Form Module
When Type Statements are placed in the Standard Code Module, they are Public by default; If Type
Statements are placed at the Form/Module Level, they must be declared as Private
Example of a Type Statement in a Form Module:
Private Type …… ……….. End Type
Accessing Information with User-Defined Data Types
Each field of data within a User-Defined Data Type is referred to as an Element of the Data Type
To access the Elements use the dot notation, similar to that used for objects
Specify [Variable.Element]
The For Each/Next Statement cannot be used with an array of User-Defined Types; Instead use a For/Next loop to keep track of the number of Elements used
A variable, which is not an array, does not need an Index
VariableName.Data Type ElementName
For an array of variables, not only has the Element of the Array to be specified, but also the Element within
the Data Type
ArrayName(Index/Subscript).Data Type ElementName
If a Data Type Statement has a number of Elements referenced by the same name (Array), there must be
Index used to specify the Element within the Data Type
VariableName.Data Type ElementName(Index/Subscript) ArrayName(Index/Subscript).Data Type
ElementName(Index/Subscript)
Filling the UDT Array with Data
Multidimensional Arrays
• Can have more than 2 dimensions
• In general an array with m rows and n columns is called an m by n array
Multi-Dimensional Arrays
May be used when Two Subscripts are required to identify tabular data, therefore data are arranged in rows
and columns
To define a Two-Dimensional Array, the Dim Statement specifies the number of rows and columns in
the Array
The row is horizontal and the column is verticalrow
row
row column column column column
Dim ArrayName([LowerLimit To] Upperlimit, [LowerLimit To] Upperlimit) As Data Type
Dim stName(2, 3) As String Dim stName(0 To 2, 0 To 3) As String
These statements establish an Array of 12 Elements (with 3 rows and 4 columns)
Two Subscripts must always be used when referring to individual Elements of the Array/Table
Row:specified by the first SubscriptColumn: specified by the second Subscript
(0,0)
(1,0)
(2,0)
(0,3)(0,1) (0,2)
(1,2)(1,1) (1,3)
(3,3)(2,2)(2,1)
The Elements of the Array may be used in the same ways as any other variable
Some valid references to this table would include:
Invalid references for an Array/Table would include any value GREATER THAN 2 for the first Subscript, or
GREATER THAN 3 for the second Subscript
Initialising Two-Dimensional Arrays
Although Numeric Array Elements are initially set to ‘zero’ and String Elements are set to ‘empty strings’, many situations require that Array Element Values are
re-initialised to ‘zero’ or some other value
Nested For/Next or For Each/Next loops can be used to set each Array Element to an initial value
Dim iRow As IntegerDim iCol As Integer
For iRow = 1 To 3For iCol = 1 To 4
ArrayName(iRow, iCol) = “”Next iCol
Next iRow*********************************************
Dim vName As VariantFor Each vName In ArrayName
vName = “”Next vName
Dim stName( ) As String Dim cBalance( ) As Currency Dim iCollected( ) As Integer
Arrays which are dimensioned with empty parenthesis, are referred to as Dynamic Arrays
A Dynamic Array is dimensioned with empty parenthesis, and the number of Elements in the Array
may change during program execution by using the Redim statement
Type NameOfNewDataTypeList of Fields
End Type
Type ProductstDescription As StringstProductNumber As StringiQuantity As IntegercPrice As Currency
End Type
Once you have created your own Data Type, you may use it to declare variables just as you would use any other Data Type
Type Statements can appear only at the Module Level in the General Declarations section of a Standard Code
Module or a Form Module
When Type Statements are placed in the Standard Code Module, they are Public by default; If Type
Statements are placed at the Form/Module Level, they must be declared as Private
Example of a Type Statement in a Form Module:
Private Type …… ……….. End Type
Multidimensional Arrays
Multidimensional Arrays
Multidimensional Arrays
Multidimensional Arrays
Multidimensional Arrays
The Array FunctionInitialising an Array at Run-Time can be time
consuming to code
VB provides another method of initialising an Array, that uses a Variant Variable
The Variable is declared to be an Array and assigns a list of values to it with the Array Function
Variable = Array(List of Values)
Dim vCodes As Variant
vCodes = Array(45, 76, 53, 24, 69)
The values within the array are accessed with the same subscript notation as a dimensioned array
What action would the assignment statement perform?