user-defined data types

33
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.

Upload: len

Post on 01-Feb-2016

71 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: User-defined Data Types

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.

Page 2: User-defined Data Types

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

Page 3: User-defined Data Types

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

Page 4: User-defined Data Types

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

Page 5: User-defined Data Types

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

Page 6: User-defined Data Types

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

Page 7: User-defined Data Types

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

Page 8: User-defined Data Types

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)

Page 9: User-defined Data Types
Page 10: User-defined Data Types

Filling the UDT Array with Data

Page 11: User-defined Data Types

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

Page 12: User-defined Data Types

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

Page 13: User-defined Data Types

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)

Page 14: User-defined Data Types

The Elements of the Array may be used in the same ways as any other variable

Some valid references to this table would include:

stName(1, 2) = “Value”stName(iRowIndex, iColumnIndex) = “Value”

txtDisplay.Text = stName(1, 2)

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

Page 15: User-defined Data Types

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

Page 16: User-defined Data Types

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

Page 17: User-defined Data Types
Page 18: User-defined Data Types
Page 19: User-defined Data Types
Page 20: User-defined Data Types
Page 21: User-defined Data Types
Page 22: User-defined Data Types
Page 23: User-defined Data Types
Page 24: User-defined Data Types

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

Page 25: User-defined Data Types

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

Page 26: User-defined Data Types

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

Page 27: User-defined Data Types

Multidimensional Arrays

Page 28: User-defined Data Types

Multidimensional Arrays

Page 29: User-defined Data Types

Multidimensional Arrays

Page 30: User-defined Data Types

Multidimensional Arrays

Page 31: User-defined Data Types

Multidimensional Arrays

Page 32: User-defined Data Types

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)

Page 33: User-defined Data Types

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?

Printer.Print vCodes(3)