Futurebasic/Language/Reference/begin union

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Begin Union[edit | edit source]

Statement[edit | edit source]

✔ Appearance ✔ Standard ✔ Console

Syntax[edit | edit source]

begin record recordName
  dim statements...
  begin union
    dim statements
  end union
end record

Description[edit | edit source]

A union is used to set aside space in a record that may potentially contain more than one size variable. The following example sets aside two equal offsets within a record for variables of differing sizes:

begin record RecordWithUnion
  dim beforeUnion
  begin union
    dim inUnion1`
    dim inUnion2$
  end union
end record
dim myTest as RecordWithUnion
myTest.inUnion2$ = "COW"
print myTest.inUnion1

The variable myTest.inUnion1 is a single byte which occupies the same space as the first byte in the string myTest.inUnion2$. In this case, myTest.inUnion1 happens to be the length byte of the string and the print statement will produce "3". Such an overlap is not necessary and the two values may have no relation to one another except that they start at the same location in memory.

When FB encounters a begin union statement, all dims up to the end union statement are examined and the largest item in the union determines the amount of space set aside by the compiler. In the example above, the union would occupy 256 bytes since the largest element in the union is a 256 byte Pascal string.

Notes[edit | edit source]

No special notes.

See Also[edit | edit source]

dim record...dim end record; dim; begin record

Language Reference