Arrays. Declaring a Array With subscript: –Dim numbers(2) as Integer –Using variable as subscript: Dim arrayIndex as Integer = 10 Dim myArray(arrayIndex)

Slides:



Advertisements
Similar presentations
Practical Programming COMP153-08S Lecture: Repetition Continued.
Advertisements

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.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
VB.Net Loops.
VB Collection Data Structures. Array ArrayList HashTable VB6 Collection Others:SortedList, Stack, Queue.
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.
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.
VB.Net Loops. Loop FOR index – start TO end [STEP step] [statements] [EXIT FOR] NEXT index DO [{WHILE| UNTIL} condition] [statements] [EXIT DO] LOOP.
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.
Chapter 8 Arrays and More.
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.
Using Arrays and File Handling
CSCI Chapter 8 Arrays Instructor: Bindra Shrestha University of Houston – Clear Lake.
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Group of variables that have a similar type
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1.
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.
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.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Arrays and More CHAPTER 8. INTRODUCTION Arrays are like groups of variables that allow you to store sets of similar data A single dimension array is useful.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
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.
Chapter 8 Arrays and More. Chapter 8 Arrays and More.
Chapter 6: Using Arrays.
Array ISYS 350.
IS 350 Arrays.
Single Dimensional Arrays
IS 350 Loops.
Chapter 8 Arrays, Timers, and More.
Array ISYS 350.
Array ISYS 350.
للمزيد زورونا على موقعنا الإلكتروني:
Array ISYS 350.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
Array ISYS 350.
Object Oriented Programming in java
Starting Out with Programming Logic & Design
Arrays.
Array ISYS 350.
Basic Collections.
Presentation transcript:

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.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 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