Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 STARTING...
-
Upload
leslie-donovan -
Category
Documents
-
view
215 -
download
2
Transcript of Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 STARTING...
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1
STARTING OUT WITH
Visual Basic 2008FOURTH EDITION
Tony GaddisHaywood Community College
Kip IrvineFlorida International University
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter
Arrays, Timers, and More8
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 3
Introduction Arrays are like groups of variables that allow you to store
sets of similar data A single dimension array is useful for storing and
working with a single set of data A multidimensional array can be used to store and
work with multiple sets of data Array programming techniques covered
Summing and averaging all the elements in an array Summing all the columns in a two-dimensional array Searching an array for a specific value Using parallel arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Arrays8.1
An Array Is Like a Group of Variables With One Name
You Store and Work Values in an Array by Using a Subscript
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 5
Array Characteristics
An array stores multiple values of same type Like a group of variables with a single name For example, the days of the week might be:
a set of 7 string variables with a maximum length of 9 characters
All variables within an array are called elements and must be of the same data type
You access the elements in an array through a subscript
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 6
Subscript Characteristics
A subscript, also called an index, is a number that identifies a specific element within an array
Subscript numbering works like a list box index: Subscript numbering begins at 0 1st element in an array is always subscript 0 Last element is total number of elements – 1
An array with 7 elements refers to the 1st element as subscript 0 and the last element as subscript 6
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 7
Declaring an Array
ArrayName is the variable name of the array UpperSubscript is the value of the array's
highest subscript Must be a positive whole number
DataType may be any Visual Basic data type VB knows an array is declared when name is
followed by parentheses with number of elements
Dim ArrayName(UpperSubscript) As DataType
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 8
Array Declaration Example
intHours(0) intHours(1) intHours(2) intHours(3) intHours(4) intHours(5)
Dim intHours(5) As Integer
Note that 6 elements are available when an array with an upper subscript of 5 is declared
Each element is an integer type
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 9
Default Initialization
All elements of an Integer array are initialized to zero Same initialization as an integer variable
Each array element is initialized exactly the same as a simple variable of that data type A Single is initialized to zero (0.0) A String is initialized to nothing (empty string)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 10
Implicit Array Sizing & Initialization An array can be initialized when declared Example:
This array is implicitly sized Upper subscript value is left blank Number of elements implied from initialization Upper subscript of 5 implied by this example This results in a 6 element array
Elements are assigned the values shown
Dim intNumbers() As Integer = {2, 4, 5, 7, 9, 12}
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 11
Named Constant as Upper Subscript
A named constant may be used as an array's highest subscript instead of a number
This is a common use for named constants Highest subscript is often used multiple times If highest subscript changes, use of a named
constant allows it to be changed in one place
Const UPPER_SUB As Integer = 100Dim array(UPPER_SUB) As Integer
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 12
Working With Array Elements Simply include the subscript to use an array
element like an ordinary variable intNumbers(2) refers to 3rd element of arrayintNumbers(0) = 100intNumbers(1) = 200intNumbers(2) = 300intNumbers(3) = 400intNumbers(4) = 500intNumbers(5) = 600
decPay = intHours(3) * decRate
intTallies(0) += 1
MessageBox.Show(decPay(5).ToString())
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 13
Arrays and Loops Loops are frequently used to process arrays Use a loop to prompt for 10 values
Use a loop to reset values in the strNames array to an empty string
Dim intNbrs(9) as IntegersDim intCount As IntegerFor intCount = 0 To 9 intNbrs(intCount) = CInt(InputBox("Enter number"))Next intCount
Dim strNames(999) as StringDim intCount As IntegerFor intCount = 0 To 999
strNames(intCount) = ""Next intCount
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 14
Array Bounds Checking Visual Basic checks each subscript value of
each array reference used at run time A subscript is invalid if it is
Negative Greater than the UpperSubscript declared
An invalid subscript causes VB to throw a run-time exception
Bounds checking is not done at design time
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 15
For Each Loop Array Processing A For…Each loop is similar to a For…Next Iterates for each element in the array strName is assigned the value of the next array
element at each iteration
Tutorial 8-1 demonstrates array processing
Dim strEmployees As String() = {"Jim", "Sally", _"Henry", "Jean", "Renee"}
Dim strName As StringFor Each strName In strEmployees
MessageBox.Show(strName)Next strName
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 16
Use For Each To Sum an Array
Start of iteration 1 intVal = 10 intSum = 0 End of iteration 1 intVal = 10 intSum = 10
Start of iteration 2 intVal = 20 intSum = 10 End of iteration 2 intVal = 20 intSum = 30
Start of iteration 3 intVal = 90 intSum = 30 End of iteration 3 intVal = 90 intSum = 120
End of iteration 4 intVal = -20 intSum = 120 Start of iteration 4 intVal = -20 intSum = 100
Private intArray() as Integer = {10, 20, 90, -20)Dim intSum as Integer = 0For Each intVal as Integer in intArray intSum = intSum + intValNext
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 17
Use For Each To Find Largest Value
Start of iteration 1 intVal = 10 intLargest = 10 End of iteration 1 intVal = 10 intLargest = 10
Start of iteration 2 intVal = 20 intLargest = 10 End of iteration 2 intVal = 20 intLargest = 20
Start of iteration 3 intVal = 90 intLargest = 30 End of iteration 3 intVal = 90 intLargest = 90
End of iteration 4 intVal = -20 intLargest = 90 Start of iteration 4 intVal = -20 intLargest = 90
Dim intArray() as Integer = {10, 20, 90, -20)Dim intLargest as Integer = intArray(0)For Each intVal as Integer in intArray If intVal > intLargest Then intLargest = intValNext
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
More About Array Processing8.2
Arrays Have Many Uses & Many Programming Techniques Can Be Applied to
For Example, Arrays Are Used to Total Values or Search for Data
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 19
Determining Number of Elements Arrays have a Length property that holds the
number of elements in the array
Note that length is number of array elements, not the upper subscript of the array
Length property always 1 greater than the upper subscript
Dim intValues(25) As Integer
For intCount = 0 to (intValues.Length – 1)MessageBox.Show(intValues(intCount).ToString)Next intCount
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 20
Total the Values in an Array Variable to hold sum (intTotal)is initialized to
zero prior to loop Iterate over all elements, adding each element to intTotal
Dim intUnits(24) as Integer ‘Declare arrayDim intTotal As Integer = 0 'Initialize accumulatorDim intCount as Integer ‘Declare loop counter
‘Find total of all values held in arrayFor intCount = 0 To (intUnits.Length – 1)
intTotal += intUnits(intCount)Next intCount
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 21
Average the Values in an Array Similar to previous example but then divides total
by number of elements to get averageDim intUnits(24) as Integer ‘Declare arrayDim intTotal As Integer = 0 'Initialize accumulatorDim dblAverage as Double ‘Declare average varDim intCount as Integer ‘Declare loop counter
‘Find total of all values held in arrayFor intCount = 0 To (intUnits.Length – 1)
intTotal += intUnits(intCount)Next intCount
‘Use floating-point division to compute averagedblAverage = intTotal / intUnits.Length
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 22
Find Highest & Lowest Array Values Pick first element as the highest Search rest of array for higher values If a higher value is located, save the value
Use similar logic to find lowest value
Dim intNumbers(24) as IntegerDim intCount as IntegerDim intHighest as Integer = intNumbers(0)
For intCount = 1 To (intNumbers.Length - 1)If intNumbers(intCount) > intHighest Then
intHighest = intNumbers(intCount)End If
Next intCount
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 23
Copy Values in One Array to Another Must copy each element one at a time
Note that this cannot be done by a simple assignment intNewValues = intOldValues Causes intNewValues array to reference
the same memory location as intOldValues Thus any change to intOldValues will
affect intNewValues and vice versa This is because arrays are object variables
For intCount = 0 To intOldValues.Length -1 intNewValues(intCount) = intOldValues(intCount)Next intCount
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 24
Parallel Arrays
Sometimes it’s useful to store related data in two or more arrays called parallel arrays Causes the nth element of one array to be
related to the nth element of another Allows related data to be accessed using the
same subscript on both arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 25
Parallel Arrays Example
Assume the following array declarations
Element 1 refers to the same person with name in one array and address in the other
Dim strNames(4) As StringDim strAddresses(4) As String
strNames(0) strNames(1) strNames(2) strNames(3) strNames(4)
strAddresses(0) strAddresses(1) strAddresses(2) strAddresses(3) strAddresses(4)
Person #1 Person #2 Person #3 Person #4 Person #5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Use parallel array processing to add name and address of each person to a list box
Tutorial 8-2 has an example of parallel arrays
Slide 8- 26
Parallel Arrays Processing
Dim strNames(4) As StringDim strAddresses(4) As String
For intCount = 0 To 4lstPeople.Items.Add("Name: " & strNames(intCount) _
& " Address: " & strAddresses(intCount))Next intCount
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 27
Array & List Box Parallel Processing
' Initialize a List Box with nameslstPeople.Items.Add("Jean James") ' Subscript 0lstPeople.Items.Add("Kevin Smith") ' Subscript 1lstPeople.Items.Add("Joe Harrison") ' Subscript 2' Initialize an Array with corresponding phone numbersphoneNumbers(0) = "555-2987"phoneNumbers(1) = "555-5656"phoneNumbers(2) = "555-8897"
' Process a selectionIf lstPeople.SelectedIndex > -1 And _
lstPeople.SelectedIndex < phoneNumbers.Length ThenMessageBox.Show(phoneNumbers(lstPeople.SelectedIndex))
ElseMessageBox.Show("That is not a valid selection.")
End If
A list box selection used as an array index
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 28
Searching Arrays, The Search Find 1st instance of value 100 in array intScores intPosition gives position of this valueDim blnFound as Boolean = FalseDim intCount as Integer = 0Dim intPosition as Integer = -1
‘ Search for a 100 in the arrayDo While Not blnFound And intCount < scores.Length
If intScores(intCount) = 100 ThenblnFound = TrueintPosition = intCount
End IfintCount += 1Loop
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 29
Searching Arrays, The Result
Indicates whether the value was found If found, position is given as a test number
' Was 100 found in the array?If blnFound Then
MessageBox.Show( _"Congratulations! You made a 100 on test " & _(intPosition + 1).ToString, "Test Results")
ElseMessageBox.Show( _
"You didn’t score a 100, but keep trying!", _"Test Results")
End If
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 30
Sorting an Array Arrays have a Sort method Arranges elements in ascending order (lowest to
highest) Sort so intNumbers order is {1, 3, 6, 7, 12}
Sort so strNames order is {Alan, Bill, Kim, Sue}
Dim intNumbers() As Integer = { 7, 12, 1, 6, 3 }Array.Sort(intNumbers)
Dim strNames() As String = { "Sue", "Kim", _"Alan", "Bill" }
Array.Sort(strNames)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 31
Dynamically Sizing an Array
ReDim is a new keyword Used to change nbr of elements at run time
If Preserve is specified, the existing contents of the array are preserved
Arrayname names the existing array UpperSubscript specifies the new highest
subscript value Can declare an array with no subscript and state
number of elements later with ReDim
ReDim [Preserve] Arrayname(UpperSubscript)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 32
Resizing Example
Dim sngScores() As Single ' Declared with no elementsDim intNumScores as Integer
' Obtain number of elements from the userintNumScores = CInt(InputBox("Enter number of test scores"))If intNumScores > 0 Then
ReDim sngScores(intNumScores - 1)Else
MessageBox.Show("You must enter 1 or greater.")End If
Array sngScores declared with no elements User prompted for number of elements ReDim resizes array based on user input
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Procedures and Functions That Work With Arrays8.3
You Can Pass Arrays As Arguments to Sub Procedures and Functions
You Can Also Return an Array From a Function
This Allows You to Write Sub Procedures and Functions That Perform General Operations With Arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 34
Passing Arrays as Arguments
Dim intNumbers() As Integer = { 2, 4, 7, 9, 8, 12 }DisplaySum(intNumbers)
Sub DisplaySum(ByVal intArray() As Integer)Dim intTotal As Integer = 0 ' AccumulatorDim intCount As Integer ' Loop counterFor intCount = 0 To (intArray.Length - 1)
intTotal += intArray(intCount)NextMessageBox.Show(“Total is " & intTotal.ToString)
End Sub
Array intNumbers passed to DisplaySum sub
Sub computes & shows sum of array elements Can pass any integer array to DisplaySum
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 35
Passing Arrays: ByVal and ByRef When passing an array ByVal
Calling procedure “sees” sub procedure changes to element values
Simple variables don’t work this way If sub assigns array argument to another array,
no effect on array in calling procedure When passing an array ByRef
Calling procedure “sees” sub procedure changes to element values
If sub assigns array argument to another array, calling procedure array values affected
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 36
Passing Arrays Example
Dim intNumbers() As Integer = { 1, 2, 3, 4, 5 }ResetValues(intNumbers)
Sub ResetValues(ByVal intArray() As Integer)Dim newArray() As Integer = {0, 0, 0, 0, 0}intArray = newArrayEnd Sub
After ResetValues procedure executes, intNumbers array still contains { 1, 2, 3, 4, 5 }
If array passed ByRef, intNumbers array will contain { 0, 0, 0, 0, 0 } after procedure runs
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 37
An Array Returned From a Function
Function GetNames() As String()' Get four names from user' Return them as an array of strings.Dim strNames(3) As StringDim strInput As StringDim intCount As IntegerFor intCount = 0 To 3 strInput = InputBox("Enter name " & _
(intCount + 1).ToString) strNames(intCount) = strInputNextReturn strNames
End Function
Return type String() indicates array of strings returned Thus the function result must be assigned to an array
of strings
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Multidimensional Arrays8.4
You May Create Arrays With More Than Two Dimensions to Hold
Complex Sets of Data
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 39
A Two Dimensional Array Picture
Column 0 Column 1 Column 2 Column 3
Row 0
Row 1
Row 2
Thus far, arrays have been one-dimensional Arrays can also be two-dimensional A two-dimensional array looks like a spreadsheet
with rows and columns
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 40
Two Dimensional Array Syntax
UpperRow and UpperColumn give the highest subscript for the row and column indices of the array
The array on the previous slide could be:
Defines three rows; 0, 1, and 2 And four columns; 0, 1, 2, and 3
Dim ArrayName(UpperRow, UpperColumn) As DataType
Dim array(2,3) As Single
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 41
Two Dimensional Array Subscripts
Dim array(2,3) As Single
Column 0 Column 1 Column 2 Column 3
Row 0 sngScores(0,0) sngScores(0,1) sngScores(0,2) sngScores(0,3)
Row 1 sngScores(1,0) sngScores(1,1) sngScores(1,2) sngScores(1,3)
Row 2 sngScores(2,0) sngScores(2,1) sngScores(2,2) sngScores(2,3)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 42
Nested Loops And Two Dimensions
For intRow = 0 To 2For intCol = 0 To 3
intNum = Val(InputBox("Enter a score."))sngScores(intRow, intCol) = intNum
Next intColNext intRow
Must use two subscripts to indicate a single element in a two dimensional array
Nested loops are often used to specify subscripts for two-dimensional array processing
For example, a nested loop below is used to insert a value into every element of array scores
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 43
Implicit Sizing and Initialization
Can be used with multi-dimensional arrays:
Row 0 values Row 1 values Row 2 values
Initializes array intNumberswith the following values
Dim intNumbers(,) As Integer = _{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }
Col 0 Col 1 Col 2
Row 0 1 2 3
Row 1 4 5 6
Row 2 7 8 9
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 44
Sum Columns, 2 Dimensional Array
Code below uses nested loops to compute sum of elements in each column of a 2 dimensional array
Outer loop controls column subscriptDim intValues(,) As Integer = {{1, 2, 3, 4}, _
{5, 6, 7, 8}, _{9, 10, 11, 12}}
For intCol = 0 to 3intTotal = 0 ‘Initialize accumulatorFor intRow = 0 to 2 ‘Sum rows in this column
intTotal += intValues(intRow, intCol)next intRow‘Display the sum of the columnMessageBox.Show(“Column “ & intCol.ToString() _
& “ sum is “ & intTotal.ToString)Next intCol
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 45
Three-Dimensional Arrays & Beyond
VB allows arrays of up to 32 dimensions Can think of a 3
dimensional arrayas having multiple pages of two dimensional arrays
Arrays beyond three dimensions are difficult to visualize
But, all one needs to do is to be consistent in the use of the different indices
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Enabled Property, Timer Control, and Splash Screens
8.5You Disable Controls by Setting Their Enabled
Property to False
The Timer Control Allows Your Application to Execute a Procedure at Regular Time Intervals
Splash Screens Are Forms That Appear As an Application Begins Executing
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 47
Enabled Property
Most controls have an Enabled property If this Boolean property is set to false the control
is disabled meaning the control: Cannot receive the focus Cannot respond to user generated events Will appear dimmed, or grayed out
Default value for this property is true May be set in code when needed as shown:
btnExample.Enabled = False
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 48
Timer Control A Timer control generates Tick events
Double-click on Timer tool in Toolbox to add Appears in component tray Standard prefix is tmr
Can perform a process at regular intervals by coding the process in a Tick event procedure
Two important properties Enabled must be true to generate Tick events Interval (milliseconds) decides tick frequency
Interval of 1000 causes 1 timer tick per second Tutorial 8-5 demonstrates the Timer control
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 49
Splash Screens A form, often with an application logo, that is
displayed while an application is loading A splash screen usually:
Has Topmost property set to True so it is displayed over the forms of other applications
Disappears shortly by using a Timer Is modeless so the application continues to
load while the splash screen is displayed Tutorial 8-6 demonstrates a splash screen
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Anchoring and Docking Controls8.6
Controls Have Two Properties, Anchor and Dock, That Allow You to Determine the Control’s Position on the Form When
the Form Is Resized at Run Time
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 51
Anchor Property Anchor - a property of various form controls A control anchored to a form edge keeps the
same distance to the form edge Controls may be anchored to any, all, or none
of the right, left, top, or bottom edges Default is to anchor to top and left edges Can just as easily anchor to bottom right If anchoring to opposing sides, control is
resized when form is resized Anchoring to all 4 sides causes control to be
resized proportionally to form
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 52
Dock Property Dock - a property of various form controls A control docked to a form edge is placed
directly against the form edge Controls may be docked to
Any one form edge but not more than one No edge or not docked (the default)
Length or width of docked control changes to match length or width of the form edge
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Random Numbers8.7
Visual Basic Provides the Tools to Generate Random Numbers
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 54
Initial Random Number Generation
Often used for games and simulations Must create an instance of the Random class to
use random number methods and properties
May supply a seed number as below so the same set of random numbers are generated every time
If no seed number is supplied when instantiated: VB uses its default random number seed Causes each sequence to be unique
Private rand as New Random(25)
Private rand as New Random
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 55
Generating More Random Numbers
The Next method returns the next integer in a random number sequence
Returns integer between 0 and 2,147,483,647 Repeatedly use a statement such as the following
to generate additional random numbers
Dim intNum as Integer = rand.Next()
intNum = rand.Next()
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 56
Random Numbers in a Range Can set an upper limit for the number generated
by specifying the limit as an argument Following example generates a random
number between 0 and 99
Lower limit of a random number is always 0 Can add or subtract to change this lower limit Following example generates a random
number between -50 and +49
intNum = rand.Next(100)
intNum = rand.Next(100) - 50
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 57
Random Numbers of Type Double Use NextDouble method to generate random
numbers of type double Returns floating point number from 0.0 to 1.0
For a random double between 0.0 and 500.0
For a random double between 100.0 and 600.0
Tutorial 8-7 shows an interesting use of random numbers
Dim dblNum as Double = rand.NextDouble()
dblNum = rand.NextDouble() * 500
dblNum = (rand.NextDouble() * 500) + 100
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Building the Demetris Leadership Center Application
8.8
Build an application making use of arrays and a splash screen
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Demetris Leadership Center Form
Slide 8- 59
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Demetris Leadership Center Form
Publisher of books, audiocassettes, and videos
Product info stored in parallel arrays
Input boxes used to prompt for sales, product, & quantity
Sales displayed as shown on right
Slide 8- 60
SplashScreen