60 likes | 159 Views
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
E N D
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 • 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
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 • PublicStructure Customer • Dim Name AsString • Dim Phone AsString • Dim Address AsString • Dim ID AsInteger • EndStructure
Arrays of Structures Public NumberCustomers AsInteger 'dynamic array of customers Public Customers() As Customer • NumberCustomers += 1 • Dim c As Customer • With c • .Name = tbCustomerName.Text • .Phone = tbCustomerPhone.Text • .Address = tbCustomerAddress.Text • .ID = NumberCustomers • tbCustomerID.Text = .ID • EndWith • ReDimPreserve Customers(NumberCustomers) • Customers(NumberCustomers) = c
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)