5
Structures and Arrays Collections of Variables

Structures and Arrays

Embed Size (px)

DESCRIPTION

Structures and Arrays. Collections of Variables. Arrays. An array is a collection of variables All with same name All with same data type Accessed by integer index Example Dim grades(20) as integer creates an array of 21 integer variables : grades(0) . . . grades(20) Example - PowerPoint PPT Presentation

Citation preview

Page 1: Structures and Arrays

Structures and Arrays

Collections of Variables

Page 2: Structures and Arrays

Arrays

• An array is a collection of variables– All with same name– All with same data type– Accessed by integer index

• Example– Dim grades(20) as integer creates an array of 21 integer

variables : • grades(0) . . . grades(20)

• Example– Dim grades() as integer defines grades as dynamic array– Redim preserve grades(numGrades) adds a variable

grades(numGrades) to collection without changing the first numGrades -1

Page 3: Structures and Arrays

Structures

• A structure is a Named collection of variables with– Different names– Different data types– Accessed by <StructureVariable>.<VariableName> where

<StructureVariable> has been defined as type with Structure name.

• A structure defines a new data type• Example

– 'structure - collection of customer attributes– Public Structure Customer– Dim Name As String– Dim Phone As String– Dim Address As String– Dim ID As Integer– End Structure

Page 4: Structures and Arrays

Arrays of Structures

• NumberCustomers += 1• Dim c As Customer• With c• .Name = tbCustomerName.Text• .Phone = tbCustomerPhone.Text• .Address = tbCustomerAddress.Text• .ID = NumberCustomers• tbCustomerID.Text = .ID• End With• ReDim Preserve Customers(NumberCustomers)• Customers(NumberCustomers) = c

Public NumberCustomers As Integer 'dynamic array of customers Public Customers() As Customer

Page 5: Structures and Arrays

Comma Separated Strings

• Let txtLine be a string read in from a text file– txtLine = “Brown,32 W Spring,372-3662

• The Split method may be used to separate the fields of txtLine :– Dim fields() as string– fields = string.split(“,”c)