VB Data Structures

Dynamic Arrays These are arrays whose size changes during program execution. Their size may not be specified at the point of their declaration. To change the size of an array, the ReDim statement is used. This is to tell the program that you are ReDimensioning the array to give it a new size (either expanding it or reducing the size).

Syntax: [Public|Private|Dim] ArrayName As Datatype ReDim ArrayName(Size) As DataType Public, Private and Dim define the scope of accessing the array e.g global or local. ArrayName is the name of the array (must adhere to the rules of naming identifiers).The ArrayName is succeddded with brackets with no value in them. DataType is the type of data to be stored.

Example: Dim studNames As String ReDim studNames(1 to 5) As String

Array Dimensions
When you declare an array, you are basically telling the program the number of memory spaces needed for the array elements. The size of computer memory to be set aside will also be determined by the data type assigned to the array.

An array dimension is the memory situation of an array and is usually denoted by a table with rows and columns.

A one dimensional array is made up of one row and several columns (or one column and several rows). Each element in the array is referenced to by specifying its index (which is a single value in this case). Example: Syntax: Access specifier Arrayname(size) As Data type or Dim Arrayname(size) As Datatype Code Example 1: Dim student (1 to 5) As String Code Example 2: Public Sub cmdEvenNumbers_KeyPress(KeyASCII As Integer) Dim studNames(1 to 7) As String Dim i As Integer for i = 1 to 7 Step 1 studNames(i) = InputBox("Enter Name for Student" & i, "Name Entry") next i Rem outputting in an array of text boxes For i = 1 to 7 txtStudDetails(i)=studNames(i) Next i End Sub A two dimensional array is denoted as several rows and columns. Its elements are referenced to by specifying the row number(index) and the column index.

Syntax: Dim Arrayname(size, size) As Datatype Example: Dim StudNames(4,5) As String Dim studMarks (1 to 4, 1 to 5) As Integer Example 1: Public sub myTwoDArray Dim Students(1 to 5, 1 to 4) As String Students(1,1) = "Jane" Students(1,2) = "Jane" Students(1,3 = "Jane" Students(1,4) = "Jane"  Students(2,1) = "Jane"  Students(2,2) = "Jane"  Students(2,3) = "Jane"  Students(2,4) = "Jane"     :     :  Students(5,4) = "Jane" End sub Example 2: Private Sub cmdMarks_Click Dim studMarks(4,6) As Integer, i, x As Integer for i = 0 to 3  for x = 0 t0 5   studMarks(i,x) = InputBox("Enter For Row" & i "Column " & x, "Marks")  next x Next i Three Dimensional Array: This is an array that has more than two dimensions, it contains several two dimension arrays.

Syntax: Dim ArrayName(size1,size2, size3) As DataType Dim ArrayName(LB1 to UB1, LB2 to UB2, LB3 to UB3) As DataType Example: Public Sub threeDiArrays dim myNumbers(4,5,1 to 3) As Integer dim i, j, x As Integer for i = 0 to 3 for j = 0 to 4 for x = 1 to 3 myNumbers(i,j,x) = i*j*x print myNumbers(i,j,x), next x       print next j     print next i End Sub

Applying the Split Function
The split function can be used to separate strings and store the separated strings into an array.

Syntax: Dim myList As String myList = "Hallo, my Computer, I Care Because You Do, ooh my"

It can be easy just to take the list and store it in an array by using Visual Basic's built in string functions. However, writing and updating such a code could prove to be time consuming and tedious. Visual Basic 6.0 provides a built in function called split that allows us to easily parse out information from a string and place it into an array.

Syntax: ArrayName = split(Sting Input[, Delimiter[, Length Limit[, Compare Mode]]]) String Input: "It is the string that you want to parse." Delimiter: "It is an optional parameter that indicates the type of string that separates the elements in the input string. This parameter is set to ' '(Null) by default. That would mean an input string of 'This is my cup' would yield an array of 4 elements ('This', 'is', 'my', 'cup')."

Length Limit: "It is the maximum size that the output array can be. The text remaining to be parsed will be set as the final element in the array."

Compare Mode:"By default, Visual Basic compares the strings character by character using their ASCII values. However, you can use different modes, this will cause Visual Basic to compare strings differently i.e vbTextCompare causes string comparisons to be case insensitive. This parameter effects how the Delimiter parses Input String."

Example:

Dim splitString As String Dim myList As String Dim i As Integer myList = "Hallo, my Computer, I Care Because You Do, ooh my" splitString= Split(myList, ", ") For i = LBound(splitString) To UBound(splitString) MsgBox splitString(i) Next

Using the Join Function
It allows us to take arrays and make them into one long string. The join function is a very simple function to implement.

Syntax:

StringName = join(Array Input[, Delimiter])

Array Input is the array you want to place into the string. Delimiter is an optional parameter that indicates what you want to place between elements are added to the string. By default this parameter is set to "".

Example: Dim studNames(0 to 6) As String, lngPosition as Long studNames(0) = "Ben" studNames(1) = "Johanna" studNames(2) = "Sam" studNames(3) = "Somson" studNames(4) = "Edwin" studNames(5) = "Carolyn" studNames(6) = "Kate" Dim myStudents As String 'This will produce the following string: "Ben, Johanna, Sam, Somson, Edwin, Carolyn, Kate" myStudents= Join(studNames, ", ")

MsgBox myStudents

Erasing an Array
It is good practice that you always erase your array when you are done using it, especially if you are using dynamic arrays. The process of erasing is rather easy. The Erase is used in such a case.

Syntax: Erase ArrayName Example:

Dim stdNames(0 to 2) As String stdNames(0) = "Binyavanga" stdNames(1) = "Murabata" stdNames(2) = "Jenefer" Erase stdNames 'Erasing the Array named stdNames

VB Enumeration Type
Enum (enumeration) is a data structure that you define when you need a set of constant values to be used in the program.

Visual Basic 6.0 allows the creation of enumerations. These are programmer-defined data types that can store a set of constant values. The use of enumeration types may simplify some programming tasks and make the program code easier to read.

Syntax: Enum EnumerationName Members End Enum EnumerationName: It is the name of the enumeration and must be specified(is required). Members: This is a required list of member constants being declared within the enum statement. Multiple members appear on individual code lines.

Each member follows the syntax:"[ ] member name [ = initializer ]" If the member is not initialized, then the default zero will be assigned to the first member. Example 1: Enum MakeOfCar Nissan Hummer Volvo Landrover Chevrolet End Enum Private Sub cmdVehicle_Click MsgBox(MakeOfCar.Volvo) End Sub

Enums are a data type, therefore, you can declare your own variables specifying an Enum as the data type.

Example 2: Dim theCar As MakeOfCar theCar = MakeOfCar.Landrover MsgBox(theCar.ToString) Example 3: Public Enum Beverages bvTea  = 10 bvCoffee = 15 bvCocoa =19 bvChocolate = 12 End Enum

See http://msdn.microsoft.com/en-us/library/8h84wky1%28v=vs.80%29.aspx for reference on Enum Type

VB Type declaration (VB Records)
Visual Basic provides a way of implementing data structures like records by use of the Visual Basic Type definition (declaration). This is a technique applied in VB that allows the programmers to define a data types that exactly meet the needs of the program. A Type definition can contain two or more individual data items(fields) that can be of different types, such as String and Integer and / or can even contain other UDTs and arrays. The programmer can also create arrays of a User Defined Type.

To define a User Defined Type, use the Type ... End Type statement, this statement must also appear in the declarations section of a code module. The individual items or members to be contained within a Type are then specified within the declaration statement.

Syntax: Public Type Identifiername field As data type field As data type :   field As data type end type Example: Public Type Student FirsthisName As String LasthisName As String DateOfBirth As Date feePaid As Currency studNumber As Integer End Type Once you define the type, you can then create its instances in a procedure / function just like you declare any other data type.

Example: Dim stud1 As Student ' This is an instance of our user defined type named Student Dim AllStudents(500) As Student

To access the members of a Type definition, then you use the TypeName.Member notation. Examples: stud1.FirsthisName= "James Mena" stud1.LasthisName= "Justus Mbau" AllEmployees(1).FirsthisName= "Miriam Makena" AllEmployees(1).LasthisName= "Kates Malibou"

Nesting User Defined Types can be useful in some situations.

Example: Public Type Person FirstName As String LasthisName As String End Type Public Type Employee hisName As Person DateOfHire As Date Salary As Currency EmployeeNumber As Integer End Type

When Types are nested, you use the same TypeName.Member notation but with an extra level applied, unlike in the past case. This will appear like this: Dim stud1 As Employee stud1.TypeName.FirstName= "Jonah"

You can also use the User Defined Types for arguments to functions and procedures and as the return type for functions. User Defined Types can also be helpful when storing data to disks, this is because VB's Random file type is specifically designed to work with User Defined Types.

User Defined Types may be declared only at the module-level, this means they can not be declared within a function or procedure. Type declarations may have Public (project-level) or Private (module-level) scope. If the keyword Public or Private is omitted, the default is Public. Type declarations with Public scope may only be defined in standard modules, not forms.

The syntax for defining a User Defined Type is: [Public | Private] Type TypeName Variable_1 As Data Type ...  ...   Variable_n As Data Type End Type For example, to define a UDT for an employee record, you might code the following: Public Type EmployeeRecord myEmpName As String dtmHireDate As Date sngHourlyRate As Single End Type However, the Type definition alone is not enough. This is because, Type definition is basically a "template" on which other variables are defined; the template itself does not store any data. To use a User Defined Type, you must define a variable "As" the TypeName following the keyword "Type" (in this case, our type is called "EmployeeRecord").

Example: Dim EmpRec As EmployeeRecord The above defines a variable called "EmpRec" which acquires the attributes defined in the structure "EmployeeRecord". Therefore, it is "EmpRec" which you refer to in your procedure / function statements and NOT "EmployeeRecord". To reference an individual element of the structure, you must qualify that particular element with the "User Defined Type" variable that you defined in the Type definition.

Example: 'the following code places data in the individual elements of EmpRec EmpRec.myEmpName= "Janet Mansen" EmpRec.dtmHireDate = #1/15/2011# EmpRec.sngHrlyRate = 35.50 You can declare any number of variables "As" the User Defined Type that you have defined.

Example: Dim EmpRec2 As EmployeeRecord Dim EmpRec(1 To 10) As EmployeeRecord : myEmpRec(5).myEmpName= "BILL JONES" Note:

The definition above declares an array of the EmployeeRecord type. That refers to a specific field (such as myEmpName) in a particular record number. You can also have an array at the elementary level.

Example: Public Type EmployeeRecord myEmpName As String dtmHireDate As Date sngHourlyRate As Single dblQuarterlyEarnings(1 To 4) As Double End Type If you have this declaration: Dim EmpRec As EmployeeRecord Then a valid reference to EmpRec from within the code would be: Print EmpRec.dblQuarterlyEarnings(2) because EmpRec is a single record If you have this declaration: Dim EmpRec(1 To 5) As EmployeeRecord Then a valid reference to EmpRec from within the code would be: Print EmpRec(3).dblQuarterlyEarnings(2) because EmpRec is an array of records You can have nested User Defined Types. Consider the declarations below: Public Type EmployeeName FirsthisName As String MidInitial As String LasthisName As String End Type Public Type EmployeeRecord udtEmphisName As EmployeeName dtmHireDate As Date sngHourlyRate As Single dblQuarterlyEarnings(1 To 4) As Double End Type

Private Sub cmdEmployment_Click Dim EmpRec As EmployeeRecord 'You would reference the Employee TypeName fields as follows EmpRec.udtEmphisName.FirstName EmpRec.udtEmphisName.MidInitial EmpRec.udtEmphisName.LastName End Sub