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.

■ RECORDS<br />

In this text, we have worked with numbers, strings, arrays, <strong>and</strong> now fixed-length strings. Strings<br />

<strong>and</strong> numbers are built-in data types that can be used without being declared, although we have<br />

always elected to declare numeric <strong>and</strong> string variables using Dim statements. On the other<br />

h<strong>and</strong>, arrays <strong>and</strong> fixed-length strings are user-defined data types that must be declared with a<br />

Dim statement before being used. A record is a user-defined data type that groups related variables<br />

of different types.<br />

Figure 8-1 shows an index card that can be used to hold data about colleges. The three<br />

pieces of data—name, state, <strong>and</strong> year founded—are called fields. Each field functions like a<br />

variable in which information can be stored <strong>and</strong> retrieved. The length of a field is the number<br />

of spaces allocated to it. In the case of the index card, we see that there are three fields<br />

having lengths 30, 2, <strong>and</strong> 4, respectively. The layout of the index card can be identified by a<br />

name, such as collegeData, called a record type.<br />

Name: State: Year Founded:<br />

FIGURE 8-1 An Index Card Having Three Fields<br />

For programming purposes, the layout of the record is declared by a block of statements similar<br />

to<br />

Type collegeData<br />

nom As String * 30<br />

state As String * 2<br />

yearFounded As Integer<br />

End Type<br />

User-Defined Data Types 247<br />

Each character of a string is stored in a piece of memory known as a byte. Therefore, a<br />

field of type String * n requires n bytes of memory. However, numbers (that is, the integer<br />

or single-precision numbers we use in this text) are stored in a different manner than strings.<br />

Integer numbers always use two bytes of memory, whereas single-precision numbers always<br />

use four bytes of memory.<br />

<strong>Visual</strong> <strong>Basic</strong> requires that Type declarations, such as the preceding record structure collegeData,<br />

be placed in either (General) or in a special file, referred to as a BAS Module.<br />

When placed in the (General) portion of a form, the word “Type” must be preceded by “Private”<br />

<strong>and</strong> the record type is only valid for that form. When placed in a BAS module, the word<br />

“Type” may be preceded by either “Private” (valid for the current BAS module) or “Public”<br />

(valid throughout the entire program). In this text, we will primarily place our declarations<br />

inside BAS Modules <strong>and</strong> make them Public.<br />

To create a BAS Module for the program currently being designed, press Alt/P/M <strong>and</strong><br />

double-click on Module. A window like the one in Figure 8-2 will appear. This window is<br />

where our Type declarations will be entered. To switch between this BAS Module window<br />

<strong>and</strong> the form(s), press Ctrl+R to activate the Project Explorer <strong>and</strong> double-click on a form or

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

Saved successfully!

Ooh no, something went wrong!