visualbasic8

advertisement
Arrays
One dimensional arrays
Index of projects
•
•
•
•
•
•
Random picture display
Sum array values
Display names in listbox
Name search
Largest/smallest
Car sales
arrays
• Arrays are multi-entry data structures.
• One dimensional arrays have a fixed length
associated with the array name, once they have
been built.
– For this reason (one-dim) arrays are called “linear”
data storage
– For this reason arrays are called “static” data storage
– the allocation may not be changed later.
• One dimensional arrays have a datatype
associated with them, the type they may hold.
For this reason arrays are called
“homogeneous”.
Arrays
• Arrays are declared specifying a size.
• Examples might be:
Dim calendarEntries(365) as Date
Dim bestFriends(3) as String
Dim list(5) as integer
Dim values(25) as Double
• Arrays can be allocated at compile time, in which
case their size is calculated based on how many
entries are provided, as in
Dim numbers as double()={1.1,2.2,3.3}
arrays
• Because arrays have fixed size, once
allocated, loops are useful for processing
them.
• Array indices are zero-based (count
subscripts starting at zero) just as String
indices are in VB.
• There are a number of fields/methods
useful for arrays such as: length, getType,
initialize(), reverse(), lastIndexOf
A loop is generally used to process
an array
‘ if we have built an array:
Dim list(5),i as integer
‘ then later….
For i=0 to list.length-1
‘ process elements here… for example
List(i)=0
‘a typical initialization
Next
We could “glue together” names
Dim longstring as string=“ “
Dim picnames As String() = {"lisa", "bart", "dollars", "robot“_
,"booper", "shakespeare", "nerd"}
For i=0 to picnames.length-1
Longstring = longstring & “ “ & picnames(i)
Next
We could search for a name
Dim picnames As String() = {"lisa", "bart", "dollars", "robot“_
,"booper", "shakespeare", "nerd"}
Dim name as string
Dim found as boolean=false
Name=txtinput.text
For i=0 to picnames.length-1
If name= picnames(i) then
Found=true
End if
Next
A form which displays random
pictures
• Uses our array of names since I found
those gif files.
• Uses techniques we used in our “flags-ofthe-world” application to display pictures
A form which displays random
pictures
Clicking random button
This form uses
• A string array declared as a field value and
initialized at time of declaration
• A random object to pick random entries –
remember parens are used when allocating an
object with the new keyword
• A picture box to display pictures
• Gets the image from a file using system.io
Dim randomguy As Random = New Random()
Dim picnames As String() = {"lisa", "bart", "dollars", "robot", "booper",
"shakespeare", "nerd"}
Random class
• The next (or nextint()) function gets the
next random value.
• You can give the next a start and final
value and it will pick a random int from
start to final-1
• If you use random class to generate reals,
it gives reals in the range (0,1). To get
other reals multiply and shift.
Code for btnrandom
Private Sub btnRandom_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btnRandom.Click
Dim val As Integer
val = randomguy.Next(0, picnames.Length)
picture.Image =
Image.FromFile(directory.GetCurrentDirectory &
picnames(val) & ".gif")
End Sub
Debugger note
• In VB 2003 the debugger expects files to
be named “bin”&name.gif since the debug
version of your project is in a bin
subdirectory
• We noticed in VB2005 it didn’t work the
same.
More on arrays and the debug
window
For each item in array…next
• For each is a useful mechanism when the
number of items in an array is not known:
Private Sub Btndisplay_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btndisplay.Click
Dim Name As String
For Each Name In names
Debug.WriteLine(Name) ’write to debug
window
Next
End Sub
Now display to listbox
Array allocation and button code
Dim names As String() = {"bob", "sue", "mary",
"jeremiah", "opie", "jezebel"} ‘put on one line or use
line continue
‘this is compile time array allocation
Private Sub Btndisplay_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btndisplay.Click
Dim Name As String
For Each Name In names
Debug.WriteLine(Name)
lstdisplay.Items.Add(Name)
Next
End Sub
Common activites
• Arrays often need to be searched for a specific
value
• Largest or smallest value may be sought.
• Arrays may need to be summed (if contents are
numeric)
Allocate an array and sum its
contents
Dim values As Double() = {1.2, 2.3, 3.4, 5.6, 6.7, 7.8, 8.9, 9.0}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim sum As Double = 0 'set sum to zero…additive identity element
Dim guy As Double
For Each guy In values
sum += guy
Next
MessageBox.Show("sum is" & sum, "The sum",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
End Sub
Find the largest (or smallest)
Revised loadform code
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim big As Double = values(0) 'set big to first one
Dim guy As Double
For Each guy In values
If guy > big Then
big = guy
End If
Next
MessageBox.Show("largest is" & big, "findbiggest",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
End Sub
Look for mary
Mary found
Look for bozo
Can’t find him
One version of search code: remember, the user
may need a reminder about case-sensitivity
Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnfind.Click
Dim search, guy As String
Dim found As Boolean = False
search = txtguy.Text ‘value in textbox
For Each guy In names
If guy = search Then
MessageBox.Show(search & "is found", "results",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
found = True
End If
Next
'after loop you'll know if he's not there
If Not found Then
MessageBox.Show(search & "is not found", "results",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If
End Sub
Another version of search code
Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnfind.Click
Dim search, guy As String
Dim found As Boolean = False
search = txtguy.Text ‘value in textbox
For Each guy In names
If guy = search Then
found = True
End If
Next
'after loop you'll know if he's not there
If found Then
MessageBox.Show(search & "is found", "results",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
else
MessageBox.Show(search & "is not found", "results",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If
End Sub
Yet another version of search code
Private Sub btnfind_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnfind.Click
Dim search, guy As String
Dim message as String = “Not there” ‘ must assume
won’t be found
search = txtguy.Text ‘value in textbox
For Each guy In names
If guy = search Then
Message=“Is there”
End If
Next
'after loop you'll know if he's not there…no if needed
MessageBox.Show(search & message, "results",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
End Sub
Car sales
We have several employees and
car models
• This application uses a combobox
• You can populate it from the properties
window by clicking the ellipsis and
entering strings into the dialog box
combobox
combobox
Arrays needed for car sales
‘which one?
Dim list As Integer() = {0, 0, 0, 0}
‘ name of car sold
Dim values(4) As String
’car costs
Dim prices as
double={50000,200000,30000,12000}
‘worker names
Dim employees() as String={“names”,”go”,”here”}
Keep track of individual employee sales
Download