Tutorial 8: Example Use of a twodimensional Array (taken fromProgramming in Visual
Basic .NET, by Bradley and Millspaugh)
1) You are going to create a project that looks up the driving distance between two cities.
Use two dropdown lists that contain the names of the cities as indicated in the table
below. Label one list “Departure” and the other “Destination. Use a button click event to
calculate the distance. Use a two dimensional array to store the distances.
2) Your GUI will look as follows:
3) Table of driving distances follows:
Boston Chicago Dallas Las Los Miami New Tor Van D.C
Vegas Angeles Orleans onto cover
Boston 0 1004 1753 2752 3017 1520 1507 609 3155 448
Chicago 1004 0 921 1780 2048 1397 919 515 2176 709
Dallas 1753 921 0 1230 1399 1343 517 1435 2234 1307
Las 2752 1780 1230 0 272 2570 1732 2251 1322 2420
Vegas
Los 3017 2048 1399 272 0 2716 1858 2523 1278 2646
Angeles
Miami 1520 1397 1343 2570 2716 0 860 1494 3447 1057
New 1507 919 517 1732 1858 860 0 1307 2734 1099
Orleans
Tor 609 515 1435 2251 2523 1494 1307 0 2820 571
onto
Van 3155 2176 2234 1322 1278 3447 2734 2820 0 2887
cover
D.C. 448 709 1307 2420 2646 1057 1099 571 2887 0
4) Properties Plan:
Properties Plan for Tutorial 8
Object Property Setting
Form1 Name Form1
Text Driving Distance
AcceptButton btnLookUp
Label1 Text Departure:
Label2 Text Destination:
cboRow Name cboRow
Items Boston
Chicago
Dallas
Las Vegas
Los Angeles
Miami
New Orleans
Toronto
Vancover
Washington, DC
DropDownStyle DropDownList
cboCol Name cboCol
Items Boston
Chicago
Dallas
Las Vegas
Los Angeles
Miami
New Orleans
Toronto
Vancover
Washington, DC
DropDownStyle DropDownList
Label3 Text Distance in Miles:
lblDistance Name lblDistance
Text
btnLookUp Name btnLookUp
Text &Distance
btnClear Name btnClear
Text C&lear
btnExit Name btnExit
Text E&xit
5) Code
' Description: Look up the driving distance in miles between
' a departure city and a destination city. Make use of
' two dimensional array that acts as a lookup table.
'
Public Class Form1
Inherits [Link]
' Declare modulelevel variable
' Declare the 2Dimensional array that will hold the distances
' between cities
Dim distanceInteger(,) As Integer = { _
{0I, 1004I, 1753I, 2752I, 3017I, 1520I, 1507I, 609I, 3155I, 488I}, _
{1004I, 0I, 921I, 1780I, 2048I, 1397I, 919I, 515I, 2176I, 709I}, _
{1753I, 921I, 0I, 1230I, 1399I, 1343I, 517I, 1435I, 2234I,1307I}, _
{2752I, 1780I, 1230I, 0I, 272I, 2570I, 1732I, 2251I, 1322I, 2420I}, _
{3017I, 2048I, 1399I, 272I, 0I, 2716I, 1858I, 2523I, 1278I, 2646I}, _
{1520I, 1397I, 1343I, 2570I, 2716I, 0I, 860I, 1494I, 3447I, 1057I}, _
{1507I, 919I, 517I, 1732I, 1858I, 860I, 0I, 1307I, 2734I, 1099I}, _
{609I, 515I, 1435I, 2251I, 2523I, 1494I, 1307I, 0I, 2820I, 571I}, _
{3155I, 2176I, 2234I, 1322I, 1278I, 3447I, 2734I, 2820I, 0I, 2887I}, _
{448I, 709I, 1307I, 2420I, 2646I, 1057I, 1099I, 571I, 2887I, 0I}}
Private Sub btnLookUp_Click(ByVal sender As [Link], ByVal e
As [Link]) Handles [Link]
' Look up driving distances between two selected cities
' Departure city selected from the departure combo box
' determines the row index into the distanceInteger array
' declared as a modulelevel variable. The destination
' city selected from the destination combo box determines
' the column index into the distanceInteger array. Both
' are used as indexes to determine the correct look up
' value.
Dim departureIndexInteger As Integer
Dim destinationIndexInteger As Integer
departureIndexInteger = [Link]
destinationIndexInteger = [Link]
If departureIndexInteger <> 1 And destinationIndexInteger <>
1 Then
[Link] = distanceInteger(departureIndexInteger,
destinationIndexInteger).ToString("N")
Else
[Link]("Select the destination and departure
city.", "Information Missing", _
[Link], [Link])
End If
End Sub
Private Sub btnClear_Click(ByVal sender As [Link], ByVal e
As [Link]) Handles [Link]
' Remove the selection from the departure and destination
' combo boxes and clear the driving distance
[Link] = 1
[Link] = 1
[Link] = ""
End Sub
Private Sub btnExit_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
' Terminate the program
[Link]()
End Sub
End Class