19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

184 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

COMMENTS<br />

1. In the discussion <strong>and</strong> examples of control arrays, the initial index was always 0.<br />

For a particular application it may be more natural to have the lowest index of<br />

a control array be 1 or even 1995. To achieve this when creating just the first<br />

element at design time <strong>and</strong> the remaining controls at run time, set the Index<br />

property of the first element to the desired lowest index value at design time,<br />

then Load the other elements using the desired indexes at run time. (The Load<br />

statement copies the properties of the element with the lowest index, whatever<br />

that lowest index may be.) For example, at design time you might create<br />

txtSales(1995) <strong>and</strong> then at run time execute the statements<br />

For yearNum = 1996 to 2005<br />

Load txtSales(yearNum)<br />

Next yearNum<br />

To create an entire control array at design time with indexes starting at a value<br />

other than 0, first create the control array using an initial index of 0. Once all<br />

elements have been created, use the Properties window to adjust the index of<br />

each element of the control array, starting with the element having the highest<br />

index.<br />

6.4 SORTING AND SEARCHING<br />

A sort is an algorithm for ordering an array. Of the many different techniques for sorting an<br />

array we discuss two, the bubble sort <strong>and</strong> the Shell sort. Both require the interchange of values<br />

stored in a pair of variables. If var1, var2, <strong>and</strong> temp are all variables of the same data type<br />

(such as all String), then the statements<br />

temp = var1<br />

var1 = var2<br />

var2 = temp<br />

assign var1’s value to var2, <strong>and</strong> var2’s value to var1.<br />

EXAMPLE 1<br />

Write a program to alphabetize two words supplied in text boxes.<br />

SOLUTION:<br />

Private Sub cmdAlphabetize_Click()<br />

Dim firstWord As String, secondWord As String, temp As String<br />

‘Alphabetize two words<br />

firstWord = txtFirstWord.Text<br />

secondWord = txtSecondWord.Text<br />

If firstWord > secondWord Then<br />

temp = firstWord<br />

firstWord = secondWord<br />

secondWord = temp<br />

End If<br />

picResult.Cls<br />

picResult.Print firstWord; “ before ”; secondWord<br />

End Sub<br />

[Run, type the following text shown into the text boxes, <strong>and</strong> click the comm<strong>and</strong> button.]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!