You are on page 1of 28

1

17
Flag Quiz Application
Introducing one-dimensional arrays
and comboboxes
2

Objectives

In this tutorial you will learn:


■ Create and initialize arrays
■ Store information in an array
■ Refer to individual elements of an array
■ Sort arrays
■ Use ComboBoxes to display options in a drop-
down list
■ Replace characters in a String
3

Test-Driving the Flag Quiz Application

A geography teacher would like to quiz students on their knowledge of


the flags of various countries. The teacher has asked you to write an
application that displays a flag and allows the student to select the
corresponding country from a list. The application should inform the user
of whether the answer is correct and display the next flag. The
application should display the fie flags randomly chosen from the flags of
Australia, Brazil, China, Italy, Russia, South Africa, Spain and the United
States . When the application is run, a given flag should be displayed
only once
4

Test-Driving the Flag Quiz Application

Flag Quiz application


5

Introducing Arrays

■ An Array is a group of variables that all contain data


items of the same name and type

■ To refer to a specific location in an array, you specify


the position number
6

Declaring Arrays

■ To declare an array, you provide the array’s name and


data type
Dim my_array As Integer ()

■ Before you can use an array, you must specify its size
and allocate memory for it
my_array = New Integer (0 To 12) {}

■ If you know the Array Bound During declaration


Dim my_array(0 To 12) As Integer
7

Declaring Arrays
■ Initialize the array
Dim my_array As Integer ()
my_array = New Integer () {10, 50, 60, 40}

■ Initialize the array during declaration


Dim my_array As Integer()= _
New Integer (0 To 3) {10, 50, 60, 40}

■ It can also be written as


Dim my_array As Integer()= {10, 50, 60, 40}

■ It can also be written as


Dim my_array As Integer()= _
New Integer (3) {10, 50, 60, 40}
8

Declaring Arrays
■ It can also be written as
Dim my_array() As Integer= {10, 50, 60, 40}
9

Common Programming Error


• Attempting to access elements in the array by using
an index outside the array bounds results in an
IndexOutOfRangeException

•If you specify an upper bound when initializing an


array. It is a compilation error if you provide too many
or two few values in the initializer list
10

Error-Prevention Tip
•Use Methd GetUpperBound when you need to find an
array’s highest index. Using an actual numerical value
for the upper bound instead could lead to errors if you
change the number of array elements

•It is important to note the difference between the


seventh element of the array and array element seven.
Array indices begin at 0 which means that the former
has index 6, whereas the latter has the index 7. This
confusion is a common source of “off-by-one” errors
11

Constructing the Flag Quiz Application


When the Form loads:
Sort the country names alphabetically
Place country names in the ComboBox
Call DisplayFlag to randomly select a flag and displays it

When the user clicks the Submit Button:


Retrieve the selected country name

if the selected value matches the correct answer


Display “correct” in the label
Else
Display “Sorry, Incorrect” in the label
If five images have been displayed
apppend “Done” to the label’s text
disable the Next flag Button and the ComboBox
Else
Enable Next Flag Button
Disable Submit Button
12

When the user clicks the next Flag Button:


Call DisplayFlag to randomly select a flag and display it
Clear the label’s text
Set ComboBox to display its first item
Update the number of flags shown
Enable Submit Button
Disable Next Flag Button

When display flag is called:


Call GetUniqueRandomNumber to obtain the index of a flag that has not yet
been used
Obtain the name of the country form the countries array
Call BuildPathName to get the image’s path and file name
Display the flag in a PictureBox
13

When GetUniqueRandomNumber is called:


Create a random object
Select the index of a flag that has not been used
Set the corresponding element of the used array to true
Return the index

When BuildPathName is called:


Use String method Repalce to remove spaces from the country string
Return a String containing the corresponding image’s path and file name
14

Initializing important variables

■ Declaring the array of country names

Creating an array of Strings


to store country names

String array that stores country names


15

Initializing important variables

■ Creating a Boolean array: it is used to keep track of


which flag has been displayed

Creating an array of Boolean


values with the same number
of elements as the array of
country names

Boolean array that keeps track of displayed flags


16

Initializing important variables

■ Initializing a counter and a variable to store the


answer

Instance variables used throughout the app.


17

Initializing important variables

■ Displaying items in the Combobox

Specifying the source of the


ComboBox items

Assigning the string elements of an array to a comboBox


18

Initializing important variables

■ Creating a procedure to build the flag-image file’s path


name

Removing whitespace from a


country name

Removing whitespace from the country name


19

Initializing important variables

■ Creating the GetUniqueRandomNumber: to select a


unique flag to display

Determining whether a
country’s flag has been
displayed previosly

Generating a unique index


20

■ Building the flag image’s path name

Getting the path name of the


flag and displaying the flag
image

Displaying a flag image


21

■ Processing a user’s answer

Retrieving the user’s answer

Determining whether the


user’s answer is correct

Submit Button Click event handler


22

■ Informing the user that the quiz is over when five flags
have been displayed

Determining if the quiz is


over

Testing whether the quiz is finished


23

■ Displaying the next flag

Displaying the next flag for


the user to identify

Next Flag button click event handler


24

■ Sorting an array

Alphabetizing country
names in the array

Sorting the array of country names


25
26
27
28

You might also like