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.

8.1 USER-DEFINED DATA TYPES<br />

Records provide a convenient way of packaging as a single unit several related variables of different<br />

types. Before we can explore this powerful variable type, we must first explore a new<br />

category of variable, the fixed-length string.<br />

■ FIXED-LENGTH STRINGS<br />

Fixed-length string variables are named following the same rules as other variable types.<br />

They are declared by statements of the form<br />

Dim var As String * n<br />

where n is a positive integer. After such a declaration, the value of var will always be a string<br />

of length n. Suppose info is an ordinary string <strong>and</strong> a statement of the form<br />

var = info<br />

is executed. If info has more than n characters, then only the first n characters will be assigned<br />

to var. If info has less than n characters, then spaces will be added to the end of the string to<br />

guarantee that var has length n.<br />

EXAMPLE 1<br />

The following program uses fixed-length strings. In the output, San Francisco is truncated to a string of<br />

length 9 <strong>and</strong> Detroit is padded on the right with two blank spaces.<br />

Private Sub cmdGo_Click()<br />

Dim city As String * 9<br />

‘Illustrate fixed-length strings<br />

picOutput.Cls<br />

picOutput.Print “123456789”<br />

city = ”San Francisco”<br />

picOutput.Print city<br />

city = “Detroit”<br />

picOutput.Print city; “MI”<br />

picOutput.Print Len(city)<br />

End Sub<br />

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

245

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

Saved successfully!

Ooh no, something went wrong!