110 likes | 117 Views
Learn how to use arrays in VB .NET to efficiently represent and access lists of values. Arrays allow you to store multiple values of the same type in a single variable.
E N D
7 – Arrays CSCI N331 VB .NET Programming Lingma Acheson Department of Computer and Information Science, IUPUI
Represent A List of Values • Sometimes we need to represent a list of values that have the similar characteristics. • Example 1: Quarterly sales figures of one year. We can use four variables to store them. dblQuarter1 = 12.8 dblQuarter2 = 15.6 dblQuarter3 = 23.2 dblQuarter4 = 18.8
Represent A List of Values • Example 2: Names of the products that a vending machine carries. We can use six variables if there are six of them. strName1 = “Pepsi” strName2 = “Sprite” strName3 = “Root Beer” strName4 = “Diet Coke” strName5 = “Mountain Dew” strName6 = “7 up”
Represent A List of Values • Example 3: Exam scores of each student in a class. If there are 28 students in the class, do we need 28 variables? Is there a better way? intScore1 = 97 intScore2 = 68 intScore3 = 82 intScore4 = 91 intScore5 = 85 intScore6 = 88 intScore7 = 72 ….
What is an array? • Yes, use an array! • Array - A data structure that holds a list of values of the same type. E.g. • Quarterly sales, an array of 4 values of type Double: • Student scores, an array of 28 values of type Integer: • Name of products in a vending machine, an array of 6 values of type String:
What is an array? • Caution – each structure is just one variable! That variable can hold multiple values. • E.g. • Quarterly sales: Name of the array: QuarterlySales How many values does the array have: 4 Type of the values: Double • Name of products in a vending machine Name of the array: Products How many values does the array have: 6 Type of the values: String
Values in an Array • Now we have a name for the whole structure, how do we refer to each value? E.g. how do I know the sales figure for the third quarter? • Answer: Use position to identify a value as array values are stored in order. • E.g. QuarterlySales: 1st element of QuarterlySales: 12.8 2nd element of QuarterlySales: 15.6 3rd element of QuaterlySales: 23.2 4th element of QuarterlySales: 18.8
Values in an Array • How do we use VB code to specify a position? Use array indexes that start from 0. E.g. QuarterlySales(0): 12.8 QuarterlySales(1): 15.6 QuarterlySales(2): 23.2 QuarterlySales(3): 18.8 E.g. ‘Initialize all the values to 0 For n = 0 To 3 QuarterlySales(n) = 0 Next
How to create an array? • Basic structure: DimarraynameAsdatatype() or: Dimarrayname() Asdatatype E.g. ‘delare an array called dblQuarterlySales that holds Double values Dim dblQuarterlySales As Double()
How to create an array? • Variations ‘declare an array with 3 as upperbound index. ‘an array of 4 elements. Dim dblQuarterlySales As Double(3) ‘initialize values when declaring the array, no need to specify a size, as four values indicate a size of 4Dim dblQuarterlySales As Double() = {12.8, 15.6, 23.2, 18.8}
Retrieve Values from an Array • E.g ‘get the first element from the array and assign it to ‘a variable dblQuarterOne = dblQuarterlySales(0) ‘display result in a textbox txtOutput.Text = “Last quarter sale: “ & _ Cstr(dblQuarterlySales(3)) ‘display all the values in a listbox For n = 0 To 3 lstOutput.Items.Add(Cstr(dblQuarteylySales(n))) Next