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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

2.4 STRINGS<br />

Two primary types of data can be processed by <strong>Visual</strong> <strong>Basic</strong>: numbers <strong>and</strong> strings. Sentences,<br />

phrases, words, letters of the alphabet, names, telephone numbers, addresses, <strong>and</strong> social security<br />

numbers are all examples of strings. Formally, a string constant is a sequence of characters<br />

that is treated as a single item. Strings can be assigned names with assignment statements,<br />

can be displayed with Print methods, <strong>and</strong> can be combined by an operation called concatenation<br />

(denoted by &).<br />

■ VARIABLES AND STRINGS<br />

A string variable is a name used to refer to a string. The allowable names of string variables<br />

are identical to those of numeric variables. The value of a string variable is assigned or altered<br />

with assignment statements <strong>and</strong> displayed with Print methods just like the value of a numeric<br />

variable.<br />

EXAMPLE 1<br />

The following code shows how assignment statements <strong>and</strong> Print are used with strings. The string variable<br />

today is assigned a value by the fourth line <strong>and</strong> this value is displayed by the fifth line. The quotation<br />

marks surrounding each string constant are not part of the constant <strong>and</strong> are not displayed by the Print<br />

method. (The form design for Examples 1 through 5 consists of a comm<strong>and</strong> button <strong>and</strong> picture box.)<br />

Private Sub cmdButton_Click()<br />

picBox.Cls<br />

picBox.Print “hello”<br />

today = “9/17/99”<br />

picBox.Print today<br />

End Sub<br />

[Run, <strong>and</strong> then click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

hello<br />

9/17/99<br />

If x, y, ..., z are characters <strong>and</strong> strVar1 is a string variable, then the statement<br />

strVar1 = “xy...z”<br />

assigns the string constant xy...z to the variable, <strong>and</strong> the statement<br />

or<br />

picBox.Print “xy...z”<br />

picBox.Print strVar1<br />

displays the string xy...z in a picture box. If strVar2 is another string variable, then the statement<br />

strVar2 = strVar1<br />

assigns the value of the variable strVar1 to the variable strVar2. (The value of strVar1 will remain<br />

the same.) String constants used in assignment or picBox.Print statements must be surrounded<br />

by quotation marks, but string variables are never surrounded by quotation marks.<br />

As with numbers, semicolons can be used with strings in picBox.Print statements to suppress<br />

carriage returns <strong>and</strong> line feeds. However, picBox.Print statements do not display leading<br />

or trailing spaces along with strings.

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

Saved successfully!

Ooh no, something went wrong!