Revision history for KeyPgUnion


Revision [22517]

Last edited on 2018-09-30 08:45:10 by fxm [Corrected typo]
Additions:
print HEX(ubgra.blue) ' Result: 44
' because it's relevant to only use a single element of a union at a given time.
Deletions:
print HEX(ubgra.blue) ' Resulr: 44
' because it's relevent to only use a single element of a union at a given time.


Revision [22516]

Edited on 2018-09-29 08:33:20 by fxm [Added one example]
Additions:
colour AS ULONG
print HEX(ubgra.colour) ' Result: 33CC66
ubgra.colour = &h228844
Deletions:
color AS ULONG
print HEX(ubgra.color) ' Result: 33CC66
ubgra.color = &h228844


Revision [22515]

Edited on 2018-09-29 08:14:19 by fxm [Added one example]
Additions:
{{fbdoc item="filename" value="examples/manual/udt/union1.bas"}}%%(freebasic)
Print userX.username ' value of username corrupted because final value assigned to posts occupies same memory location
' ' (and this is reason that value of posts is displayed well)
Print userY.posts ' value of posts corrupted because final value assigned to username occupies same memory location
' ' (and this is reason that value of username is displayed well)
{{fbdoc item="filename" value="examples/manual/udt/union2.bas"}}%%(freebasic)
' Example 2: Alternative to RGBA keyword and allowing to retrieve elementary colors values
UNION BGRA_UNION
color AS ULONG
TYPE
blue AS UBYTE
green AS UBYTE
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.color) ' Result: 33CC66
print
' Setting a ULONG value...
ubgra.color = &h228844
' We can get the individual color values
print HEX(ubgra.red) ' Result: 22
print HEX(ubgra.green) ' Result: 88
print HEX(ubgra.blue) ' Resulr: 44
print
sleep
{{fbdoc item="filename" value="examples/manual/udt/union3.bas"}}%%(freebasic)
' Example 3.
Deletions:
{{fbdoc item="filename" value="examples/manual/udt/union0.bas"}}%%(freebasic)
Print userX.username ' value of username corrupted because final value assigned to posts occupies same memory location
' ' (and this is reason that value of posts is displayed well)
Print userY.posts ' value of posts corrupted because final value assigned to username occupies same memory location
' ' (and this is reason that value of username is displayed well)
' Example 2.


Revision [22514]

Edited on 2018-09-29 06:50:30 by fxm [Changed example 1]
Additions:
' Example 1: Only one union member can be relevantly accessed at a time
Union member
username As String * 32
posts As Ulong
Dim As member userX
userX.username = "Samantha"
userX.posts = 1234
Print userX.username ' value of username corrupted because final value assigned to posts occupies same memory location
' ' (and this is reason that value of posts is displayed well)
Print userX.posts
Dim As member userY
userY.posts = 4321
userY.username = "Alexander"
Print userY.username
Print userY.posts ' value of posts corrupted because final value assigned to username occupies same memory location
' ' (and this is reason that value of username is displayed well)
Deletions:
' Example 1: bitfields.
Type unitType
Union
Dim attributeMask As ULong
Type ' 32-bit ulong can support up to 32 attributes.
isMilitary : 1 As ULong
isMerchant : 1 As ULong
End Type
End Union
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.


Revision [22513]

Edited on 2018-09-29 04:22:07 by fxm [Wording]
Additions:
' Only one field within the union is set, without choice criterion.
Deletions:
' One field set a priori.


Revision [22512]

Edited on 2018-09-28 06:33:19 by fxm [Just cleaned up examples 1 and 2]
Additions:
Unlike Type, Union can not contain variable-length strings and arrays, and more generally fields (or can not have bases) with constructors or destructors. Therefore, Union does not support to inherit from the ##[[KeyPgObject|Object]]## built-in type.
Deletions:
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 ##[[KeyPgObject|Object]]## built-in type.


Revision [22511]

Edited on 2018-09-28 01:36:57 by fxm [Just cleaned up examples 1 and 2]
Additions:
Type unitType
Union
Dim attributeMask As ULong
Type ' 32-bit ulong can support up to 32 attributes.
isMilitary : 1 As ULong
isMerchant : 1 As ULong
End Type
End Union
End Type
Dim myunit As unitType
Print myunit.isMilitary ' Result: 1.
Print myunit.isMerchant ' Result: 1.
Print myunit.attributeMask ' Result: 3.
Print
' Define a simple union.
Union AUnion
a As UByte
b As UInteger
' Define a composite type with an unnamed union.
Type CompType
s As String * 20
ui As UByte 'Flag to tell us what to use in union.
Union
au As UByte
bu As UInteger
End Type
' Flags to let us know what to use in union,
' because it's relevent to only use a single element of a union at a given time.
Const IsInteger = 1
Const IsUByte = 2
Dim MyUnion As AUnion
Dim MyComposite As CompType
' One field set a priori.
MyComposite.bu = 1500 ' Field set according to the above flag.
Print "Simple Union: ";MyUnion.a
Print MyComposite.s & ": ";
If MyComposite.ui = IsInteger Then
Print MyComposite.bu
ElseIf MyComposite.ui = IsUByte Then
Print MyComposite.au
Else
Print "Unknown Type."
End If
Print
Deletions:
type unitType
union
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
print myunit.isMilitary ' Result: 1.
print myunit.isMerchant ' Result: 1.
print myunit.attributeMask ' Result: 3.
sleep
' 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.
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.
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
else
print "Unknown type."
end if
sleep


Revision [22510]

Edited on 2018-09-26 16:05:55 by fxm [Rewording]
Additions:
The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType|Type]]##. Since they occupy the same space, only one element can usually be used at a given time.
Deletions:
The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType|Type]]##. Since they occupy the same space, only a single element can be used.


Revision [22506]

Edited on 2018-09-26 03:37:52 by fxm [Added a first simple example]
Additions:
{{fbdoc item="filename" value="examples/manual/udt/union0.bas"}}%%(freebasic)
' 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 least significant byte gets stored first).
Union UDU
ul As Ulong ' 32-bit data type
Type
ub0 As Ubyte ' 8-bit data type
ub1 As Ubyte ' 8-bit data type
ub2 As Ubyte ' 8-bit data type
ub3 As Ubyte ' 8-bit data type
End Type
End Union
Dim As UDU u
u.ul = &h12345678
Print Hex(u.ul) ' Result: 12345678
Print Hex(u.ub3), Hex(u.ub2), Hex(u.ub1), Hex(u.ub0) ' Result: 12 34 56 78
Sleep


Revision [22066]

Edited on 2017-12-31 04:04:07 by fxm [Added precision in Note]
Additions:
**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.
Deletions:
**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. Of course it can be dangerous, but that is always the problem with Unions.


Revision [22044]

Edited on 2017-12-20 16:17:12 by fxm [Added note on inheritance]
Additions:
**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. Of course it can be dangerous, but that is always the problem with Unions.


Revision [22043]

Edited on 2017-12-20 15:49:09 by fxm [Added precision on inheritance from a base]
Additions:
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 ##[[KeyPgObject|Object]]## built-in type.
Deletions:
Unlike Type, Union can not contain variable-length strings, and more generally fields (or can not have bases) with constructors or destructors.


Revision [21368]

Edited on 2016-04-15 08:12:43 by fxm [Added link to 'Coercion and Conversion']
Additions:
- [[ProPgDataConversion|Coercion and Conversion]]


Revision [21167]

Edited on 2016-03-13 11:32:30 by fxm [Formatting]
Additions:




Revision [20571]

Edited on 2016-02-10 16:10:04 by DkLwikki [Update link format]
Additions:
//fieldname// as [[DataType|datatype]]
[[KeyPgDeclare|declare]] //member function declaration// ...
Unions are similar to a ##[[KeyPgType|Type]]## structure, except that the elements of a union occupy the same space in memory.
Like Type, Union can use the optional ##[[KeyPgField|Field]] = //number//## specifier and supports also inheritance through the use of the [[KeyPgExtends|Extends]] keyword.
The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType|Type]]##. Since they occupy the same space, only a single element can be used.
Unions support member functions including ##[[KeyPgConstructor|Constructor]]##, ##[[KeyPgDestructor|Destructor]]##, ##[[KeyPgMemberFunction|Function]]##, ##[[KeyPgOperator|Operator]]##, ##[[KeyPgProperty|Property]]## and ##[[KeyPgMemberSub|Sub]]##. All members of a union are public and access control is not supported.
au as ubyte
bu as integer
- Object-related features as functions defined inside the ##**Union**## block are supported only in the //[[CompilerOptlang|-lang fb]]// dialect.
- Not available in the //[[CompilerOptlang|-lang qb]]// dialect unless referenced with the alias ##**""__Union""**##.
- ##[[KeyPgType|Type]]##
Deletions:
//fieldname// as [[DataType datatype]]
[[KeyPgDeclare declare]] //member function declaration// ...
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory.
Like Type, Union can use the optional ##[[KeyPgField Field]] = //number//## specifier and supports also inheritance through the use of the [[KeyPgExtends Extends]] keyword.
The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.
Unions support member functions including ##[[KeyPgConstructor Constructor]]##, ##[[KeyPgDestructor Destructor]]##, ##[[KeyPgMemberFunction Function]]##, ##[[KeyPgOperator Operator]]##, ##[[KeyPgProperty Property]]## and ##[[KeyPgMemberSub Sub]]##. All members of a union are public and access control is not supported.
au as ubyte
bu as integer
- Object-related features as functions defined inside the ##**Union**## block are supported only in the //[[CompilerOptlang -lang fb]]// dialect.
- Not available in the //[[CompilerOptlang -lang qb]]// dialect unless referenced with the alias ##**""__Union""**##.
- ##[[KeyPgType Type]]##


Revision [17368]

Edited on 2014-11-05 03:38:49 by FxMwikki [Local union cannot have procedures members or static data members.]
Additions:
Nested unnamed type or union cannot have procedure members or static data members (same restriction for local named type/union).
Deletions:
Nested unnamed type or union cannot have function members or static data members.


Revision [16997]

Edited on 2014-01-17 03:28:01 by FxMwikki [Added detail and formating]
Additions:
Like Type, Union can use the optional ##[[KeyPgField Field]] = //number//## specifier and supports also inheritance through the use of the [[KeyPgExtends Extends]] keyword.


Revision [16995]

Edited on 2014-01-16 16:22:40 by FxMwikki [Added detail and formating]
Additions:
Unlike Type, Union can not contain variable-length strings, and more generally fields (or can not have bases) with constructors or destructors.
Deletions:
Unlike Type, Union can not contain variable-length strings, and more generally fields or bases with constructors or destructors.


Revision [16994]

Edited on 2014-01-16 13:13:15 by FxMwikki [Added detail and formating]
Additions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory.
Unlike Type, Union can not contain variable-length strings, and more generally fields or bases with constructors or destructors.
The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.
Deletions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory (but unlike Type, Union can not contain variable-length strings, and more generally fields with constructors or destructors). The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.


Revision [16946]

Edited on 2013-09-13 05:42:00 by FxMwikki [Nested unnamed type or union also cannot have static data members]
Additions:
Nested unnamed type or union cannot have function members or static data members.
Deletions:
Unnamed type nested inside a named union cannot have function members.


Revision [16810]

Edited on 2013-05-19 13:50:16 by FxMwikki [Nested unnamed type cannot have function members]
Additions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory (but unlike Type, Union can not contain variable-length strings, and more generally fields with constructors or destructors). The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.
Unions support member functions including ##[[KeyPgConstructor Constructor]]##, ##[[KeyPgDestructor Destructor]]##, ##[[KeyPgMemberFunction Function]]##, ##[[KeyPgOperator Operator]]##, ##[[KeyPgProperty Property]]## and ##[[KeyPgMemberSub Sub]]##. All members of a union are public and access control is not supported.
Unnamed type nested inside a named union cannot have function members.
Deletions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory (but unlike Type, Union can not contain variable-length strings, and more generally fields with constructors or destructors). The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.
Unnamed unions can be nested inside a type structure and unnamed types can be nested inside an union. See Example.
Unions support member functions including ##[[KeyPgConstructor Constructor]]##, ##[[KeyPgDestructor Destructor]]##, ##[[KeyPgMemberFunction Function]]##, ##[[KeyPgOperator Operator]]##, ##[[KeyPgProperty Property]]## and ##[[KeyPgMemberSub Sub]]##. All members of a union are public and access control is not supported.


Revision [16157]

Edited on 2012-04-07 09:36:19 by FxMwikki [Union does not support variable-length strings]
Additions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory (but unlike Type, Union can not contain variable-length strings, and more generally fields with constructors or destructors). The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.
Deletions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory (but unlike Type, Union can not contain variable-length strings). The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.


Revision [16156]

Edited on 2012-04-07 04:21:43 by FxMwikki [Union does not support variable-length strings]
Additions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory (but unlike Type, Union can not contain variable-length strings). The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.
Deletions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory (also unlike Type, Union can not contain variable-length strings). The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.


Revision [16155]

Edited on 2012-04-07 04:18:13 by FxMwikki [nion does not support variable-length strings]
Additions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory (also unlike Type, Union can not contain variable-length strings). The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]##. Since they occupy the same space, only a single element can be used.
Unnamed unions can be nested inside a type structure and unnamed types can be nested inside an union. See Example.
Unions support member functions including ##[[KeyPgConstructor Constructor]]##, ##[[KeyPgDestructor Destructor]]##, ##[[KeyPgMemberFunction Function]]##, ##[[KeyPgOperator Operator]]##, ##[[KeyPgProperty Property]]## and ##[[KeyPgMemberSub Sub]]##. All members of a union are public and access control is not supported.
Deletions:
Unions are similar to a ##[[KeyPgType Type]]## structure, except that the elements of a union occupy the same space in memory. The size of the Union is the size of the largest data item. A data item can be an unnamed ##[[KeyPgType Type]]## . Since they occupy the same space, only a single element can be used.
Unnamed unions can be nested inside a type structure and unnamed types can be nested inside an union . See Example.
Unions support member functions including ##[[KeyPgConstructor Constructor]]##, ##[[KeyPgDestructor Destructor]]##, ##[[KeyPgMemberFunction Function]]##, ##[[KeyPgOperator Operator]]##, ##[[KeyPgProperty Property]]## and ##[[KeyPgMemberSub Sub]]##. All members of a union are public and access control is not supported.


Revision [16153]

Edited on 2012-04-06 09:58:36 by AgAmemnus [nion does not support variable-length strings]
Additions:
' Define a composite type.
Deletions:
'define a composite type


Revision [16152]

Edited on 2012-04-06 09:49:42 by AgAmemnus [nion does not support variable-length strings]
Additions:
' Example 1: bitfields.
type unitType
union
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
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.
' Flags to let us know what to use in union.
' You can only use a single element of a union.
' Can only set one value in union.
MyComposite.ui = IsInteger ' Tells us this is an integer union.
Deletions:
'define our union
'Flags to let us know what to use in union.
'You can only use a single element of a union.
'Can only set one value in union
MyComposite.ui = IsInteger 'Tells us this is an integer union


Revision [14459]

Edited on 2009-08-29 21:57:29 by CountingPine [Remove CamelCase links in "New to FreeBASIC"]
Additions:
- New to ""FreeBASIC""
Deletions:
- New to FreeBASIC


Revision [13376]

The oldest known version of this page was created on 2008-05-20 06:36:01 by AntoniGual [Remove CamelCase links in "New to FreeBASIC"]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode