Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question defining a packed record using the Case Statement 1

Status
Not open for further replies.

JES1904

Programmer
Jul 7, 2008
36
US
I just ran across some code that declares a complex packed record:

aSInt = Integer;
entity = packed Record
Enttype : byte;
field2 : double;
... //continues with other standard fields and then uses this case
CASE aSInt OF
entlin: (linpt1,
linpt2: point);
entln3: (ln3pt1,
ln3pt2: point);
entmrk: (mrkpnt,
mrkvec: point;
mrktyp,
mrksiz: aSInt);
... //other case types
End;

The entlin, entln3, entmrk are constants used to identify specific types. This code confuses me. The case statement is using an aSint, which is an integer, but does not assign it to a variable such as with this:
Case Etype of
entlin: (linpt1,
linpt2: point);
...//etc
end;
where Etype is a variable that conveys the type constant to apply.

How is this code used without an identification of what the aSint? How is the Asint value passed on when the record is used since it varies in size and the type of each instance must known to determine its size and allocate memory and read the data?
 
This type of record is known as a Record with Variant part.

These two links explain the situation ..




In simple terms, a record can consist of two parts - the standard part of the record (which you already understand) and a number of alternate parts. Each of these alternate parts occupies exactly the same space in the record (the space being allocated by the compiler based on the size of the largest alternate part). In this way, a single record structure can be used to hold data that differs in some way. These alternate parts are always preceeded by a Case statement - the value of which (unless the record is being stored in a database) is essentially irrelevant.

For example:

....
....
[tt]
Case integer Of
0: (WordVar : word);
1: (ByteVar1, ByteVar2 : Byte)
end;
[/tt]

This allows for two bytes of storage in the variant part, allowing you to retrieve (or store) either a single word or the constituent bytes. (As an aside that example allows you to see how Intel CPUs store numbers).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top