Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Variant records: the equivalent to the C-union structure Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
27-Dec-02
Category
Algorithm
Language
Delphi 2.x
Views
100
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Lou Adler

Is there a way to create a C 'union'-like structure in Delphi? That is, a structure 
that uses the same memory area?

Answer:

The Delphi (Pascal/ObjectPascal) equivalent to a C-union structure is called a 
Variant Record (not to be confused with the Variant "type" available in Delphi 
2.0+). As with a C- union, the Pascal variant record allows several structure types 
to be combined into one, and all will occupy the same memory space. Look up the 
syntax declaration under "Records" in the help file. But here's an example:

1   type
2     TPerson = record
3       FirstName, LastName: string[40];
4       BirthDate: TDate;
5       case Citizen: Boolean of
6         True: (BirthPlace: string[40]);
7         False: (Country: string[20];
8           EntryPort: string[20];
9           EntryDate: TDate;
10          ExitDate: TDate);
11    end;
12  
13  The record above is actually a single expression of two records that could describe 
14  a person:
15  
16  type
17    TPersonCitizen = record
18      FirstName, LastName: string[40];
19      BirthDate: TDate;
20      BirthPlace: string[40]
21    end;
22  
23    and
24  
25  type
26    TPersonAlien = record
27      FirstName, LastName: string[40];
28      BirthDate: TDate;
29      Country: string[20];
30      EntryPort: string[20];
31      EntryDate: TDate;
32      ExitDate: TDate;
33    end;


And as in a union, the combination of the two types of records makes for much more 
efficient programming, because a person could be expressed in a variety of ways.

Everything I explained above is pretty hypothetical stuff. In Delphi, the TRect 
structure that describes a rectangle is actually a variant record:

34  type
35    TPoint = record
36      X: Longint;
37      Y: Longint;
38    end;
39  
40  TRect = record
41    case Integer of
42      0: (Left, Top, Right, Bottom: Integer);
43      1: (TopLeft, BottomRight: TPoint);
44  end;


where the coordinates of the rectangle can be expressed using either four integer 
values or two TPoints.

I realize this is pretty quick and dirty, so I suggest you refer to the help file for a more in-depth explanation, or go to your nearest book store or library and look at any Pascal book (not Delphi -- most won't explain this fairly esoteric structure). However, if you're familiar with the C-union, this stuff should be an absolute breeze.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC