Presentation is loading. Please wait.

Presentation is loading. Please wait.

User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.

Similar presentations


Presentation on theme: "User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables."— Presentation transcript:

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

2 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

3 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

4 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

5 Type NameOfNewDataType List of Fields End Type Type Product stDescription As String stProductNumber As String iQuantity As Integer cPrice 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

6 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

7 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

8 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)

9

10 Filling the UDT Array with Data

11 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

12 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 vertical row column column column column

13 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 Subscript Column: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)

14 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

15 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

16 Dim iRow As Integer Dim iCol As Integer For iRow = 1 To 3 For iCol = 1 To 4 ArrayName(iRow, iCol) = “” Next iCol Next iRow ********************************************* Dim vName As Variant For Each vName In ArrayName vName = “” Next vName

17

18

19

20

21

22

23

24 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

25 Type NameOfNewDataType List of Fields End Type Type Product stDescription As String stProductNumber As String iQuantity As Integer cPrice 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

26 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

27 Multidimensional Arrays

28

29

30

31

32 The Array Function Initialising 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)

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


Download ppt "User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables."

Similar presentations


Ads by Google