Declares a union user defined type.

Union typename
fieldname as datatype
declare member function declaration ...
End Union

Name of the Union
Name of a data field member
member function declaration
Any of the supported member functions

Unions are similar to a Type structure, except that the elements of a union occupy the same space in memory.
Like Type, Union can use the optional Field = number specifier and supports also inheritance through the use of the Extends keyword.
Unlike Type, Union can not contain variable-length strings, and more generally fields (or can not have bases) with constructors or destructors. Therefore, Union does not support to inherit from the Object built-in type.
The size of the Union is the size of the largest data item. A data item can be an unnamed Type. Since they occupy the same space, only a single element can be used.

Unions support member functions including Constructor, Destructor, Function, Operator, Property and Sub. All members of a union are public and access control is not supported.

Nested unnamed type or union cannot have procedure members or static data members (same restriction for local named type/union).

A Union can be passed as a user defined type to overloaded operator functions.

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 fields 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 Union, then it won't be affected by fields from the derived UDT.
Since Union is not allowed to have complex fields (i.e. UDTs with constructor/destructor, or dynamic strings), a derived Union cannot be allowed to have (contain) a complex base.

' Example 1: bitfields.
Type unitType
  Dim attributeMask As UInteger
  Type    ' 32-bit uintegers can support up to 32 attributes.
   isMilitary         : 1 As UInteger
   isMerchant         : 1 As UInteger
  End Type
 End Union
End Type

Dim myunit As unitType
myunit.isMilitary = 1
myunit.isMerchant = 1
Print myunit.isMilitary    ' Result: 1.
Print myunit.isMerchant    ' Result: 1.
Print myunit.attributeMask ' Result: 3.

' Example 2.
' Define our union.
Union AUnion
    a As UByte
    b As Integer
End Union
' Define a composite type.
Type CompType
    s As String * 20
    ui As Byte 'Flag to tell us what to use in union.
        au As UByte
        bu As Integer
    End Union
End Type

' Flags to let us know what to use in union.
' You can only use a single element of a union.
Const IsInteger = 1
Const IsUByte = 2

Dim MyUnion As AUnion
Dim MyComposite As CompType

' Can only set one value in union.
MyUnion.a = 128

MyComposite.s = "Type + Union"
MyComposite.ui = IsInteger ' Tells us this is an integer union.
MyComposite.bu = 1500

Print "Union: ";MyUnion.a

Print "Composite: ";
If MyComposite.ui = IsInteger Then
    Print MyComposite.bu
ElseIf MyComposite.ui = IsUByte Then
    Print MyComposite.au
    Print "Unknown type."
End If


Dialect Differences:
Differences from QB:
See also:
Back to User Defined Types
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki

sf.net phatcode