VB Collection Data Structures. Array ArrayList HashTable VB6 Collection Others:SortedList, Stack, Queue.

Slides:



Advertisements
Similar presentations
VB List(Of type) A List can be thought of as an array that automatically adjusts its size as elements are added and removed A List may hold only objects.
Advertisements

Practical Programming COMP153-08S Lecture: Repetition Continued.
Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.
VB.Net Loops.
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Odds and Ends Component Tray Menu and contextmenu Splash Screen.
Coding ADO.NET Objects: Connection, Command, DataReader.
An array of controls Not particularly convenient in VB Examples: array of pictureboxes Array of textboxes Notes on Concentration game (a possible final.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.
Arrays. Declaring a Array With subscript: –Dim numbers(2) as Integer –Using variable as subscript: Dim arrayIndex as Integer = 10 Dim myArray(arrayIndex)
Coding ADO.NET Objects: Connection, Command, DataReader.
More on lists, exceptions, loops and validation. You can use the exception to indicate the error that occurred Private Sub btnCheck_Click(ByVal sender.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Classes ISYS 546. Classes A class is program structure that defines a user-defined data type that are used to create objects. An instance of a.
VB.Net Loops. Loop FOR index – start TO end [STEP step] [statements] [EXIT FOR] NEXT index DO [{WHILE| UNTIL} condition] [statements] [EXIT DO] LOOP.
Arrays One dimensional arrays. Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales.
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Using Arrays and File Handling
Week Arrays, Timers, and More 9. 2 Introduction Arrays are like groups of variables that allow you to store sets of similar data A single dimension array.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays Group of variables that have a similar type
Some graphics. Projects included A slideshow a dark moon moving phases of the moon billiards Your own icons and bitmaps Pie chart.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
Tutorial 6 The Repetition Structure
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8 Arrays and More.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Array and ArrayList ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items in an.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
Practical Programming COMP153-08S Lecture: Repetition.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Directory and File. Access Files in a Directory Name space: System.IO The Directory and File classes contain only shared methods that set or return information.
Array, ArrayList and List ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items.
VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,
Arrays 1.
Array ISYS 350.
IS 350 Arrays.
Single Dimensional Arrays
Introduction to VB programming
IS 350 Loops.
Array ISYS 350.
Array ISYS 350.
للمزيد زورونا على موقعنا الإلكتروني:
Array ISYS 350.
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Array ISYS 350.
Arrays.
Coding ADO.NET Objects: Connection, Command, DataReader
Array ISYS 350.
VB Classes ISYS 512.
Presentation transcript:

VB Collection Data Structures

Array ArrayList HashTable VB6 Collection Others:SortedList, Stack, Queue

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 = {“”, “”, “”} 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.GetUpperBound(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

Searching with the IndexOf Method If numbers.IndexOf(numbers, InputBox("Enter search value: ")) < 0 Then MessageBox.Show("not exist") Else MessageBox.Show("exist") 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 MessageBox.Show(totalSales.ToString) Problem: How to compute total sales by Department? by Product?

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

Other Collections More flexible than array: –No need to declare the number of objects in a collection. –Objects can be added, deleted at any position. –Object can be retrieved from a collection by a key. –Can store any types of data.

ArrayList Define an arraylist: –Dim myArrayList As New ArrayList() Properties:Count, Item, etc. –myArrayList.Item(0) 0-based index Methods: –Clear, Add, Insert, Remove, RemoveAt, Contains, IndexOf, etc.

ArrayList Demo Dim testArrayList As New ArrayList() Dim Fruits() As String = {"Apple", "orange", "Banana"} Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim f2 As New Form2() testArrayList.Add("David") testArrayList.Add(20) testArrayList.Add(Fruits) testArrayList.Add(f2) TextBox1.Text = testArrayList.Item(0) TextBox2.Text = testArrayList.Item(1).ToString TextBox3.Text = testArrayList.Item(2)(1) TextBox4.Text = testArrayList.Item(3).Age End Sub

For Each Loop with ArrayList Dim testArrayList As New ArrayList() Dim f2 As New DataForm2() Dim Fruits() As String = {"Apple", "orange", "Banana"} testArrayList.Add("David") testArrayList.Add(20) testArrayList.Add(Fruits) testArrayList.Add(f2) Dim myObj As Object For Each myObj In testArrayList MessageBox.Show(myObj.GetType.ToString) Next

Data Binding with ArrayLists Arraylists can be used as data source for a control. Demo: ListBox DataSource property. –Dim myArrayList As New ArrayList() – myArrayList.Add("apple") – myArrayList.Add("banana") – myArrayList.Add("orange") – ListBox1.DataSource = myArrayList

Dim wordList As New ArrayList Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load wordList.Add("This") wordList.Add("is") wordList.Add("a") wordList.Add("list") wordList.Add("of") wordList.Add("words") wordList.Add("that") wordList.Add("we") wordList.Add("will") wordList.Add("use") wordList.Add("in") wordList.Add("the") wordList.Add("spell") wordList.Add("checker") wordList.Add("david") wordList.Add("chao") wordList.Add(" ") End Sub Spelling Checker Example

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim re As Regex re = New Regex("\w+") Dim source As String source = txtSource.Text Dim mc As MatchCollection = re.Matches(source) Dim m As Match Dim result As String For Each m In mc If Not wordList.Contains(m.Value) Then result += m.ToString & vbCrLf txtSource.Select(txtSource.Text.IndexOf(m.ToString), m.Length) MessageBox.Show("next") End If Next MessageBox.Show("Invalid words: " & vbCrLf & result) Catch ex As System.Exception MessageBox.Show(ex.Message) End Try End Sub

HashTabl e The HashTable collection provides very fast look- up. Each element in a HashTable is a DictionaryEntry type containing a pair of Key and Value. Properties:Count, Item, Keys, Values –myHashTable.Item(key) Methods: –Clear, Add, Remove,ContainsKey, ContainsValue, etc. Note 1: Elements in a HashTable are stored according to the hash value of keys. Note 2: Keys must be unique.

HashTable Example Dim myHashTable As New Hashtable() Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myHashTable.Add("UK", "United Kingdom") myHashTable.Add("US", "United States") myHashTable.Add("CHN", "China") myHashTable.Add("DE", "Germany") Dim myKey As String For Each myKey In myHashTable.Keys ListBox1.Items.Add(myKey) Next End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged MessageBox.Show(myHashTable.Item(ListBox1.SelectedItem)) End Sub

Dim searchKey As String searchKey = InputBox("enter key") If myHash.ContainsKey(searchKey) Then TextBox1.Text = myHashTable.Item(searchKey) Else MessageBox.Show("key not exists") End If

VB 6 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 pcol As New ArrayList Add images to 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

Dim pcol As New ArrayList Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 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) End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Static counter As Integer Me.BackgroundImage = pcol.Item(counter) counter += 1 counter = (counter Mod 3) End Sub