Arrays in Visual Basic - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Arrays in Visual Basic

Description:

Caption = '' Label. lblPayroll. Text = '' Text Box. txtSalary. Text = '' Text ... label4.Caption = iNumbers(3) label5.Caption = iNumbers(4) End Sub. Summing up ... – PowerPoint PPT presentation

Number of Views:3078
Avg rating:5.0/5.0
Slides: 29
Provided by: compu354
Category:
Tags: arrays | basic | caption | visual

less

Transcript and Presenter's Notes

Title: Arrays in Visual Basic


1
Arrays in Visual Basic
  • Week 9
  • CM30104-1

2
What is an array ?
  • An array is a data structure that enables us to
    store a list of values that can be thought of as
    a table
  • Enables us to reference several data values by
    one variable name

3
Types of Arrays
  • There are 2 types of arrays in VB
  • Data Arrays
  • Control Arrays

4
Two types of Arrays in VB
  • Data Arrays
  • Primarily used to store data which is related in
    some way and of the same type, e.g
  • student grades for a class of twenty students
  • weather details for a series of places.
  • Control Arrays
  • A mechanism for duplicating controls and allowing
    the same event coding to be triggered by an
    action to any of the elements of the control
    array.

5
Data arrays example
iNos
42
31
98
6
Array Declaration
  • Arrays are declared in the same way as for
    variables.
  • Dim iNos (1 To 5) As Integer
  • or Dim iNos (5) As Integer

When no start number specified array index begins
at 0
7
An example - Finding largest of a
sequence of numbers
  • Using several variables iNum1, iNum2, iNum3
    etc
  • and a complicated Nested If .
  • If (iNum1 gt iNum2) And (iNum1 gt iNum2) And
  • And (iNum1 gt iNum5) Then
  • iLargest iNum1
  • Else
  • If (iNum2 gt iNum1) And (iNum2 gt iNum3)
  • .. etc

8
An example - Finding largest of a
sequence of numbers
  • Using a For loop and one variable, iNum
  • For iLoopCount 1 To 5
  • iNum InputBox (Enter next number)
  • If iNum gt iLargest Then
  • iLargest iNum
  • End If
  • Next iLoopCount

iLargest 0
9
Finding largest of a sequence of numbers -
using an Array
  • Dim iNos (1 To 5) as Integer
  • iLargest 0
  • For iCount 1 To 5
  • iNos (iCount) InputBox (Enter next number)
  • If iNos (iCount) gt iLargest Then
  • iLargest iNos (iCount)
  • End If
  • Next iCount

10
Finding largest of a sequence of marks also
storing names associated with each mark
11
Finding largest of a sequence of marks also
storing names associated with each mark
Fill the arrays first For iLoop 1 To 5
sNames(iLoop) . iMarks(iLoop) . Next
iLoop then have other loops to process the data
in the arrays
12
Arrays have many advantages e.g, we can then
sort list students in order
ARRAYS AFTER SORTING
13
Example coding
  • fill arrays
  • For iCount 1 to 5
  • sNames(iCount) InputBox(Name)
  • iMarks(iCount) InputBox(Mark)
  • Next iCount
  • display to listboxes
  • For iCount 1 to 5
  • lstNames.AddItem sNames(iCount)
  • lstMarks.AddItem iMarks(iCount)
  • Next iCount

14
Payroll example
  • Using a form like
  • this to
  • Input name salary
  • Store data in an
  • array
  • Scroll through
  • previous data
  • entered

15
Payroll example
  • Scroll bar set at
  • design stage to
  • min 0
  • max 9
  • Data stored in 2
  • arrays
  • sName (10) As String
  • sSalary (10) As Currency

16
Payroll example
Defining the variables Private sName
(10) As String Private cSalary (10) As
Currency Private ID As Integer Private
cTotalPayroll As Currency
17
Program code
Private sName (10) As String Private cSalary
(10) As Currency Private ID As Integer Private
cTotalPayroll As Currency
Private Sub cmdEmployee_Click() ID
scrEmployee.Value Set array
index
depending on scroll bar
sName(ID) txtName.Text Enter data
from text cSalary(ID) txtSalary.Text
boxes into arrays cTotalPayroll
cTotalPayroll cSalary(ID) Add to total
pay scrEmployee.Value
scrEmployee.Value 1 Move scroll bar

on 1 position End Sub
18
Program code
Private sName (10) As String Private cSalary
(10) As Currency Private ID As Integer Private
cTotalPayroll As Currency
  When the scroll bar is moved Private Sub
scrEmployee_Change() ID scrEmployee.Value
Set array index depending
on
scroll bar
Display name salary from

appropriate position of arrays txtName.Text
sNames(ID) txtSalary.Text
Format(cSalary(ID), "Currency") End Sub
19
Arrays can have more than 1 dimension
  • Occasionally information can often be presented
    more clearly by using arrays with more than one
    dimension.

Row
2
3
4
5
1
Col
100
41
18
15
13
4
20
Arrays can have more than 1 dimension
  • E.g Dim iAllMarks (1 To 5, 1 To 4) As
    Integer

columns rows
iAllMarks (2, 4) 41
21
Accessing 2-D arrays
  • Usually done using two nested loops
  • For col 1 to 5
  • For row 1 to 3
  • Store input in cell (col,row)
  • Next row
  • Next col
  • For row 1 to 3
  • For col 1 to 5
  • Store input in cell (col,row)
  • Next col
  • Next row

22
Single line or column?
  • By keeping the column number the same and varying
    row access a single column
  • e.g.txtOutput.Text marks(3,row) in loop
  • By keeping the row number the same and varying
    col access a single row
  • e.g.txtOutput.Text marks(col,1) in loop

23
Array example - sorting
In this example a simple set of inputs are set
up. Clicking the top button allows data entered
to be stored in the array The middle one sorts
the data The bottom button puts sorted data in
the text boxes
24
Set Global variables and Initialise data
Const cmin 0 Const cmax 4
declare data array Private
iNumbers(cmin To cmax) As Integer  Sub
Form_Load () Dim i As Integer
initialise array elements
to zero For i cmin To cmax
iNumbers(i) 0 Next i
initialise text boxes text1
iNumbers(0) text2 iNumbers(1) text3
iNumbers(2) text4 iNumbers(3) text5
iNumbers(4) End Sub
25
Store Numbers
Sub cmdAssign_Click () If (text1.Text "")
Or (text2.Text "") Or (text3.Text "") Or
(text4.Text "") Or (text5.Text "") Then
Beep MsgBox ("a zero
length string is present") Else
store data from
textboxes into array iNumbers(0)
CInt(text1.Text) iNumbers(1)
CInt(text2.Text) iNumbers(2)
CInt(text3.Text) iNumbers(3)
CInt(text4.Text) iNumbers(4)
CInt(text5.Text) End If End Sub
26
Sort Numbers
Sub cmdRearrange_Click () Dim i As Integer Dim
iPass As Integer Dim iTemp As Integer Dim
iNoSwitches As Integer iPass 0 Do iPass
iPass 1 iNoSwitches 1 For i cmin To
(cmax - iPass) If iNumbers(i) gt
iNumbers(i 1) Then iNoSwitches
0 iTemp iNumbers(i)
iNumbers(i) iNumbers(i 1)
iNumbers(i 1) iTemp End If
Next i Loop Until NoSwitches 1 End Sub
27
Redisplay numbers
  • Sub cmdRetrieve_Click ()
  • label1.Caption iNumbers(0)
  • label2.Caption iNumbers(1)
  • label3.Caption iNumbers(2)
  • label4.Caption iNumbers(3)
  • label5.Caption iNumbers(4)
  • End Sub

28
Summing up
  • Simple data arrays can be thought of as tables of
    data
  • Arrays enable us to reference several data items
    using one variable name
  • They can be 2-dimensional (or 3, or 4 )
  • They are almost always processed using loops
Write a Comment
User Comments (0)
About PowerShow.com