1 / 33

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.

len
Télécharger la présentation

User-defined Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 TypesInteger, String, Currency, etc…….. are what are known as VB data typesYou 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 informationFor example, a Product Data Type might contain a Product Description, a Product Number, Quantity, and PriceThe 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 Typeintroduces 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 NameOfNewDataTypeList of Fields End TypeType Product stDescription As String stProductNumber As String iQuantity As Integer cPrice As CurrencyEnd TypeOnce 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 ModuleWhen 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 PrivateExample of a Type Statement in a Form Module: Private Type …… ……….. End Type

  7. Accessing Information with User-Defined Data TypesEach field of data within a User-Defined Data Type is referred to as an Element of the Data TypeTo access the Elements use the dot notation, similar to that used for objectsSpecify [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 IndexVariableName.Data Type ElementNameFor an array of variables, not only has the Element of the Array to be specified, but also the Element within the Data TypeArrayName(Index/Subscript).Data Type ElementNameIf 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 TypeVariableName.Data Type ElementName(Index/Subscript)ArrayName(Index/Subscript).Data Type ElementName(Index/Subscript)

  9. Filling the UDT Array with Data

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

  11. Multi-Dimensional ArraysMay be used when Two Subscripts are required to identify tabular data, therefore data are arranged in rows and columnsTo define a Two-Dimensional Array, the Dim Statement specifies the number of rows and columns in the ArrayThe row is horizontal and the column is vertical row row row column column column column

  12. Dim ArrayName([LowerLimit To] Upperlimit, [LowerLimit To] Upperlimit) As Data TypeDim stName(2, 3) As String Dim stName(0 To 2, 0 To 3) As StringThese 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/TableRow: specified by the first SubscriptColumn: specified by the second Subscript (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (3,3)

  13. The Elements of the Array may be used in the same ways as any other variableSome 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 firstSubscript, or GREATER THAN 3 for the secondSubscript

  14. Initialising Two-Dimensional ArraysAlthough NumericArray 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 valueNested For/Next or For Each/Next loops can be used to set each Array Element to an initial value

  15. Dim iRow As IntegerDim iCol As IntegerFor iRow = 1 To 3For iCol = 1 To 4 ArrayName(iRow, iCol) = “”Next iColNext iRow*********************************************Dim vName As VariantFor Each vName In ArrayName vName = “”Next vName

  16. Dim stName( ) As String Dim cBalance( ) As Currency Dim iCollected( ) As IntegerArrays which are dimensioned with empty parenthesis, are referred to as Dynamic ArraysA 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

  17. Type NameOfNewDataTypeList of Fields End TypeType Product stDescription As String stProductNumber As String iQuantity As Integer cPrice As CurrencyEnd TypeOnce you have created your own Data Type, you may use it to declare variables just as you would use any other Data Type

  18. Type Statements can appear only at the Module Level in the General Declarations section of a Standard Code Module or a Form ModuleWhen 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 PrivateExample of a Type Statement in a Form Module: Private Type …… ……….. End Type

  19. Multidimensional Arrays

  20. Multidimensional Arrays

  21. Multidimensional Arrays

  22. Multidimensional Arrays

  23. Multidimensional Arrays

  24. The Array FunctionInitialising an Array at Run-Time can be time consuming to codeVB provides another method of initialising an Array, that uses a Variant VariableThe Variable is declared to be an Array and assigns a list of values to it with the Array FunctionVariable = Array(List of Values)

  25. Dim vCodes As VariantvCodes = Array(45, 76, 53, 24, 69)The values within the array are accessed with the same subscript notation as a dimensioned arrayWhat action would the assignment statement perform?Printer.Print vCodes(3)

More Related