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.

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

Care must be taken when comparing an ordinary (variable-length) string with a fixedlength<br />

string or comparing two fixed-length strings of different lengths.<br />

EXAMPLE 2<br />

In the following program, the strings assigned to the variables town, city, <strong>and</strong> municipality have lengths<br />

7, 9, <strong>and</strong> 12, respectively, <strong>and</strong> therefore are all different.<br />

Private Sub cmdGo_Click()<br />

Dim town As String * 7<br />

Dim city As String * 9<br />

Dim municipality As String * 12<br />

‘Illustrate fixed-length strings<br />

town = “Chicago”<br />

city = “Chicago”<br />

municipality = “Chicago”<br />

picOutput.Cls<br />

If (city = town) Or (city = municipality) Then<br />

picOutput.Print “same”<br />

Else<br />

picOutput.Print “different”<br />

End If<br />

picOutput.Print “123456789012345”<br />

picOutput.Print city & ”***”<br />

picOutput.Print town & “***”<br />

picOutput.Print municipality & “***”<br />

End Sub<br />

[Set picOutput’s Font property to Courier. Run, <strong>and</strong> click the comm<strong>and</strong> button.]<br />

There are times when we want to consider the values assigned to variables of different<br />

types as being the same, such as city <strong>and</strong> town in Example 2. In this situation, the function<br />

RTrim comes to the rescue. If info is an ordinary string or a fixed-length string, then the<br />

value of<br />

RTrim(info)<br />

is the (variable-length) string consisting of info with all right-h<strong>and</strong> spaces removed. For<br />

instance, the value of RTrim(“hello”) is the string “hello”. In Example 2, if the If block is<br />

changed to<br />

If (RTrim(city) = town) And (RTrim(city) = RTrim(municipality)) Then<br />

picOutput.Print “same”<br />

Else<br />

picOutput.Print “different”<br />

End If<br />

then the first line of the output will be “same”.

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

Saved successfully!

Ooh no, something went wrong!