230 likes | 340 Views
Arrays. Declaring a Array. With subscript: Dim numbers(2) as Integer Using variable as subscript: Dim arrayIndex as Integer = 10 Dim myArray(arrayIndex) as Integer Without subscript Dim numbers() as Integer = {2, 4, 6} Dim someNames() as String = {“”, “”, “”}
E N D
Declaring a Array • With subscript: • Dim numbers(2) as Integer • Using variable as subscript: • Dim arrayIndex as Integer = 10 • Dim myArray(arrayIndex) as Integer • Without subscript • Dim numbers() as Integer = {2, 4, 6} • Dim someNames() as String = {“”, “”, “”} • Note: Can not have a subscript with a initialization list. • Without subscript and initialization • Dim numbers As Integer() • numbers = New Integer() {2, 4, 6}
Accessing Array Elements with a For … Next Loop • Dim i As Integer = 0, sum As Integer = 0 • For i = 0 To 2 • sum += numbers(i) • Next • GetUpperBound • For i = 0 to numbers.GetUpperBound(0) sum += numbers(i) • Next • Length • For i = 0 to numbers.length-1 sum += numbers(i) • Next
Accessing Array Elements with a For Each Loop Dim i As Integer For Each i In numbers i = i * 2 MessageBox.Show(i.ToString) Next
Array’s Properties and Methods • Properties: • Length • IsFixedSize • IsReadOnly • Methods • BinarySearch *** return negative value if not found • Clear • Clone, Copy, CopyTo • GetLowerBound, GetUpperBound • Reverse • Sort
Highest Values in a Array Dim highest As Integer highest = numbers(0) For i = 1 To numbers.GetLowerBound(0) If numbers(i) > highest Then highest = numbers(i) End If Next
Searching Arrays Dim found As Boolean = False Dim searchValue As Integer searchValue = InputBox("Enter search value: ") For i = 0 To numbers.GetUpperBound(0) If numbers(i) = searchValue Then found = True Exit For End If Next If found Then MsgBox("Number found") Else MsgBox("Number not found") End If
Using Parallel Relationship between Array and Listbox Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListBox1.Items.Add("Peter") ListBox1.Items.Add("Paul") ListBox1.Items.Add("Mary") phone(0) = "1234" phone(1) = "6789" phone(2) = "3456" End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged MessageBox.Show(ListBox1.SelectedItem & "phone is" & phone(ListBox1.SelectedIndex)) End Sub
ReDim • ReDim numbers(5) • Original values in the array will be lost. • ReDim Preserve numbers(5) • Use ReDim to assign size if the array is declared without subscript. • Dim test As Integer() • … • ReDim test(2)
Passing Arrays as Arguments Dim outstr As String setnew(test) For i = 0 To test.GetUpperBound(0) outstr &= test(i).ToString & vbCrLf Next MessageBox.Show(outstr) End Sub Sub setnew(ByVal a() As Integer) Dim i As Integer For i = 0 To a.GetUpperBound(0) a(i) = 0 Next End Sub Note: ByVal or ByRef? With ByVal, it will prevent an array argument from being assigned to another array.
Two-Dimensional Arrays • Depts=1 • Prods=2 • Dim SalesData(Depts, Prods) As Double • With initialization • Dim SalesData(,) as Double = {{20,30,15},{40,32,55}}
For Each Loops for 2-dimensional Array Dim salesData(,) As Double = {{20, 15, 30}, {30, 21, 50}} Dim totalSales, I As Double For Each I In salesData totalSales += I Next TextBox1.Text = totalSales.ToString
For Next Loops for 2-dimensional Array Dim row, col As Integer For row = 0 To salesData.GetUpperBound(0) For col = 0 To salesData.GetUpperBound(1) totalSales += salesData(row, col) Next Next MessageBox.Show(totalSales.ToString)
Data Binding with Arrays • Connect a control to one data source. • Arrays can be used as data source for a control. • Demo: ListBox DataSource property. • Dim fruits() As String = {"Apple", "Orange", "Banana", "Strawberry", "Kiwi"} • ListBox1.DataSource = fruits
Collections • Collections are used to store lists of objects. • More flexible than array: • No need to declare the number of objects in a collection, no need to ReDim. • Objects can be added, deleted at any position. • Object can be retrieved from a collection by a key. • A collection’s name usually end with a “s”.
Using Collections • Define a collection: • Ex. Dim Pets as New Collection • Methods: • ADD: Add object to a collection • Pets.Add(“dog”) • Add an object with a key: • Pets.Add(“Dog”, “D”) • Item: Retrieve an object from a collection with a position index (base 1) or with a key. • petName = Pets.Item(1) • petName = Pets.Item(“D”) • Count: Return the number of objects in a collection. • Remove: Delete an object with a position index or key.
Iterating Through a Collection Dim Pets as New Collection … Dim Indx as Long For Indx = 1 to Pets.Count …operations … Next Indx For Each pet in Pets … operations … Next pet
Timer • Event: • Tick • Property: • Enable • Interval property • measured in millisecond, 1000 millis = 1 second • Methods: • Start • Stop
Status Bar & Timer • Status Bar • Panels property (collection) • Set ShowPanel property to true. • StatusBar1.ShowPanels = True • StatusBarPanel1.Text = System.DateTime.Now.ToString • Timer • Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick StatusBarPanel1.Text = System.DateTime.Now.ToString End Sub
Bitmap Data Type • To read a picture file to program: • Dim pic as New Bitmap(“c:\mypic.jpg”)
Rotate Form’s Background Image Create a collection of pictures: DIM PCOLAS NEW COLLECTION Dim im1 As New Bitmap("c:\Paradise.jpg") Dim im2 As New Bitmap("c:\Flyaway.jpg") Dim im3 As New Bitmap("c:\SnowTrees.jpg") pcol.Add(im1) pcol.Add(im2) pcol.Add(im3) Use Timer to change image: Me.BackgroundImage = pcol.Item(counter) counter = (counter Mod 3) + 1
Me.BackgroundImage = pcol.Item(counter) • counter = (counter Mod 3) + 1
Other Collection Classes • ArrayList • HashTable • SortedList • Stack • Queue