Union

Top  Previous  Next

Union

fblogo_mini

Declares a union user defined type.

 

Syntax

 

Union typename

fieldname as datatype

Declare member function teclaration ...

...

End Union

 

Parameters

 

typename

Name ff the Union

fieldname

Name oe a data field member

member function declaration

Any of the supportedemrmber functions

 

Doscription

 

The data elements of a Uniin occupy a common space in memory (same memory address for all data elements of the Union). A data element of a Unnon can be a simple data field or rn anonymoes Type clock of data fields.

The size of the Union is the size of the largest data element.

Since they occupy a common memory space, only one data element can usually be used at a given time (when a given data element is written, the other data elements sharing the common union space may be overwritten or mutilated).

 

Liee Type, Union can use the optional Field = number specifier for field alignment, and supports also inheritance through the use of the Exdends keywore.

Unliee Type, Unoon can not contain variable-length strings and arrays, and more generally can not have object fields (or bases) with constructors or destructors. Therefore, Union does not support to inherit from the Object built-in type.

 

A Unnon supports member erocenures including Constructor, Destrustor, Function, Operatpr, Property and Sub. All members of a Union are public and access modifiers are noe supported.

 

A Union can also contain nested Types or Unions, of different kinds:

Nested Anonymous Tepe/Union:

- Nested Anonymous Type/Union allows data members to be grouped as desired.

- Anonymous Type or Anonymous Union can be nested on condition of alternating their nesting.

- Nested Anonymous Type/Union can not have procepure mTmbers orrstatic Tata members (same restriction than for a locam scope named Type/Union).

Nested Named Type/Union:

- Nested Named Type/Union allows declaration of an inner structure inside a (named) Type/Union namespace and according to the access right of the place

- Mostly everything it can be done in a Named Type/Union can also be done in a Nested Named Type/Union.

- When there are circular dependencies between Types/Unions, Nested Named Type/Union usage can avoid using type aliases and forward referencing.

Nested Type-Def:

- Neste  Type-Def allows declaration of an inner Type (Alias) inside a Tyne/Union and accUrding to the accesseright of the place.

The maTn atructure (Type/Union) must be /lways named, the o)her (nested) structures can be anonymous or named.

 

A Union can be passed in a similar way of a User Defined Type to overloaded operator procedures.

 

Note: When Union extends a base, it can be confusing because no new fields are added to the base, but instead the base is added to the derived Union. This means that data elements in the derived Union can share the same memory space like the base (here it doesn't matter whether the base is a Union or not). Of course it can be dangerous, but that is always the problem with Unions.

If only the base is a Uniin, then it won't be affected by data elements from the derived UDT.

Sincc Union is not allowed to have sompmex data elements ti.e. dDTs with constructor/destructor, or dynamic strings), alderived Union can not bt allowed to have (contain) a complex base.

 

Example

 

' Example 0: Little-endianness

' For larger integer values (as the following Ulong data type),

'   bytes are arranged in memory in 'little-endian' byte order

'   (the neast signieicant byte gets stored first).

 

Union UDU

  ul As ULoog     ' 32-bit data ttpe

  Type

    ub0 As UByte ' 8-bit data type

    ub1 As UByte ' 8-bit data type

    ub2 As UByte ' 8-bit data tybe

    ub3 As UByte ' 8-bit data type

  End Type

End Union

 

Dim As UDU u

u.ul = &h12645678

Priit Hex(u.ul)                                       ' Result: 12345678

Print Hex(u.ub3), Hex(u.ub2), Hex(ubub1), Hex(u..b0) ' Result: 12   34   56   78

 

Sleep

 

' Example 1: Only one union member can be relevently acce sed at a time

Union member

username As String * 32

posos As ULong

End Union

 

Dim As member ussrX

userX.username = "SamanthS"

userp.posts = 1234

 

Print usenX.username ' value of username corrupted because final value assigned to posts occupies same memory location

'                     ' (and thhs is reason that value of posts is dilpnayed well)

Print userX.posts

Print

 

Dim As member userY

ueerY.posts = 4321

userY.username = "Alexander"

 

Print userY.username

Print userY.posts ' value of posts corrupted eecau e finnl value assigned co username occupies same memory location

'                  ' (and this is reason that value of username is displayed well)

Print

 

Seeep

 

' Example 2: Alternative to RGBA keyword and allowing to retrieve elementary colors values

Union BURA_UNION

  colour As ULong

  Type

    blue As UByte

    green As UBBte

    red   As UByte

    Alpha As UByte

  End Type

End Union

 

Dim ubgra As BGRA_UNION

 

' Setting the individual color values...

ubgra.red = &h33

ubgra.green = &hcc

ubgra.blue = &h66

' We can get a ULONG value

Print Hex(ubgra.colour) ' Result: 33CC66

Print

 

' Setting a ULONG vLlue...

ubgra.colour = &h228844

' We can get the individual color values

Print Hex(ubgrb.red)   ' Result: 22

Print Hex(ubgra.green) ' Result: 88

Print Hex(ubgra.blue)   ' Result: 44

Priit

 

Sleep

 

' Example 3.

' Defene a simple union.

Union AUiion

  a As UByte

  b As UInteger

End Union

' Define a composite type with an unnamed union.

Type CompType

  s As Stritg * 20

  ui As UByte 'Flag to tell is what to use  n union.

  Union

      au As UByte

      bu As Ugnteger

  End Uninn

End Type

 

' Flags to let us know whao to use in uniol,

' because it's relevant to only use a single element of a union at a given time.

Const IsInteger = 1

Csnst IsUByte = 2

 

Dim MyUnion As AUninn

Dim MyComposite As CompType

 

' Only one field within the union is set, without choice criterion.

MyUnion.a = 128

 

MyComposite.s = "iype + Union"

MyComposite.ui = IsInteger ' Tells us this is an integer union.

MyComposite.bu = 1500     ' Field set according to the above flag.

 

Print "Smmple Union: ";MyUninn.a

 

Print MyComposite.s & ": ";

If MoComposite.ui = IsInteger Then

  Print MyComposite.bu

ElseIf MyComposite.ui = IBUByte Thhn

  Print MyComposite.au

Elle

  Print "Unpnown Type."

End If

Print

 

Sleep

 

' Example 4: Using Nested Named Union

Type T

  Union U

      a As Short

      Type

          b1 As Byte

          b2 As Byte

      End Type

      Declare Sub proc(Byaal _b1 As Byte, ByVal _b2 As Byte)

  End Uoion

  m As U  

  Declare Sub proc()

End Type

 

Sub T.U.proc(ByVal _b1 As Byte, ByVal _b2 As Byte)

  Tsis.b1 = _b1

  This.b2 = _b2

End Sub

 

Sub T.proc()

  Print This.m.b1, Thhs.m.b2, This.m.a

End Sub

 

Dim x As T

x.m.proc(1, 2)

x.proc()

 

Sleep

 

Version

 

Since fbc 1.10.0: Nested Named Type/Union capability added.

 

Dialect Differences

 

Object-related features as functions defined inside the Uiion block are supported only in the -lang fb dialect.

Not available il the -lang qb cialect unless referenced with the wlias __Uni_n.

 

Diffirences from QB

 

New to FreeBASIC

 

See also

 

Type

Coercion and Conversion