Board index » delphi » Delphi equivalent to a C Union?

Delphi equivalent to a C Union?

Ok, in C you can create a "union", which is a double definition of a
particular variable (or group of variables).

Can a C programmer who knows what I'm talking about tell me what the
equivalent is in Delphi's programming language.

Thanks

Gunfighter
(Brett)

 

Re:Delphi equivalent to a C Union?


Quote
>   lathr...@netcom.com (Gunfighter) writes:

>  Ok, in C you can create a "union", which is a double definition of a
>  particular variable (or group of variables).

>  Can a C programmer who knows what I'm talking about tell me what the
>  equivalent is in Delphi's programming language.

>  Thanks

>  Gunfighter
>  (Brett)

You do a variant record which is a combination of struct and union. The first part acts like a structure, the second as a UNION.
The first part is optional, so that leaves you a pure UNION. Of course if you leave the second part you keep the regular non-variant record,
which is equivalent to struct in C

example

type
        myunion = record
                case integer of
                0: ( i : word);
                1: (j : longint);
                2: (m:float);
                end;

For more information look for  records and variant records in the help.

George Blat

Re:Delphi equivalent to a C Union?


In <lathropeDF9KBw....@netcom.com> lathr...@netcom.com (Gunfighter) writes:

Quote
>Ok, in C you can create a "union", which is a double definition of a
>particular variable (or group of variables).
>Can a C programmer who knows what I'm talking about tell me what the
>equivalent is in Delphi's programming language.

It is a variant part of a record, like this:

type
  TMyRecord = record
    Name : string;
    case integer of
      0 : (LongInfo: longint);
      1 : (PtrInto : pointer);
      2 : (LoWord : word; HiWord);
    end;

--
 Hallvard Vassbotn  | Falcon AS (a Reuters company) | Without programmers,
 hallv...@falcon.no | Stranden 1, 0250 OSLO, Norway | the world would stop!

Re:Delphi equivalent to a C Union?


   Ok, in C you can create a "union", which is a double definition of a
   particular variable (or group of variables).

   Can a C programmer who knows what I'm talking about tell me what the
   equivalent is in Delphi's programming language.

   Thanks

   Gunfighter
   (Brett)

Well, I'd resist being called a C programmer, but the standard Pascal
way of doing this is to use a variant record.  It comes in 2 flavours:

a)  The safe way has a tag value which is used to distinguish which
    actual variant of the union is in force:

TYPE SafeVariant =
        RECORD
            a, b, c: Integer;
            CASE Size:  (Large, Medium, Small) OF
                Large: (LargeString:  String[255]);
                Medium: (MediumString:  String[127]);
                Small:  (SmallString:   String[63])
        END;

The CASE is the equivalent of the union, and must be the last component
of the record.  The actual variants are the bits in brackets, and can
be any field lists you like.

b)  The yucky C-style way is to omit the tag field, and just guess
    at what type the variant actually is at any time, eg:

TYPE  VariantView = (AsBytes, AsWord);
      UnsafeVariant =
        RECORD
            CASE VariantView OF
                AsWord:  (TheWord: Word);
                AsBytes: (HiByte, LoByte: Byte)
        END;

The common, but wrong, way of doing this is to use a type that is already
lying around as the tag type, and just use arbitrary values as the
CASE branches, e.g:

TYPE
      suckyVariant =
        RECORD
            CASE Boolean OF
                True:  (TheWord: Word);
                False: (HiByte, LoByte: Byte)
        END;

{ or even }

      TotallysuckyVariant =
        RECORD
            CASE Integer OF
                9:  (TheWord: Word);
                37: (HiByte, LoByte: Byte)
        END;

But you can see what's wrong with that without my telling you.
--
-----------------------------------
Matt Francomb, Setanta Software Ltd
http://www.demon.co.uk/setanta
-----------------------------------

Re:Delphi equivalent to a C Union?


You are talking about variant records.

C version of following example:

struct Facts {
    char Name[10];
    int Kind;
    union {
      float N;
      ?     D;
      char  S[255];
    }

Quote
}

Facts is a records with 2 fixed fields and three fields that are
over-layed.

Kind is used as a tag denoting which over-layed field had data.

Hope this helps. Send me e-mail if you need more help.
I've used this extensively.

Help Topic: record

Here is Borland's example:

{ Record Type Definitions }
type
  TClass  = (Num, Dat, Str);
  TDate   = record
     D, M, Y: Integer;
  end;
  Facts = record
    Name: string[10];
    case Kind: TClass of
      Num: (N: Real);
      Dat: (D: TDate);
      Str: (S: string);
  end;

Here is Borlands help section:

Description

A record contains components, or fields, that can be of different types.
Each field list separates identifiers with a comma, followed by a colon and a type.
You must name the field and assign a field type in the record-type declaration.
The variant part of the record type syntax diagram distributes memory space for more
than one list of fields, letting you access information in more than one way. Each
field list is a variant which overlays the same space in memory. Each variant is
distinguished by a constant, and you can access all fields of all variants at all
times.
The optional identifier, the tag field identifier, is the identifier of an additional
fixed field--the tag field--of the record. The program uses the tag field's value to
show which variant is currently active.

Accessing records

You can access the whole record or each field individually. To retrieve information
from an individual field type the record name, a period, and then the field
identifier.  For example,

TDateRec.Year

If a record contains a subrecord, you can access it using qualifier.

Jim

Other Threads