Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
About Union and it's uses in FB
#1
Hello all,

I'm not sure I'm understand what is union in an UDT, how do you represent it in a statement? For E.g.:

Code:
Type UserType
     A As Long
     B As Long
     Union UserUnion
          C As Byte
          D As Short
          E As Byte
     End Union
     F As Double
End Type

DIM NewVar as UserType

NewVar.C = 10     ' Is this correct?

NewVar.UserUnion.C = 10     ' Or should be this way?

no.D
Reply
#2
You cannot have named unions inside types in FB at the moment. What you can have is anonymous unions, and they are done like this:

Quote:type Something
X as integer
Y as integer
union
I as integer
S as single
end union
end type
dim S as Something
S.X = 10
S.Y = 10
S.I = 10
S.S = 10
Reply
#3
What is it for?
img]http://img59.exs.cx/img59/7237/aqsig2el.png[/img]
Reply
#4
all union members have same address in memory, and the entire lenght in bytes of the union is the size of it's biggest field. It helps to save memory. Also modifying one you change the value of all members.

Code:
UNION MyUnion
   i AS UINTEGER
   s AS STRING * 1
   b AS UBYTE
END UNION
DIM u AS MyUnion

PRINT "Lenght of the union in bytes:"; LEN(u)
print "Before u.i=2400:"
print "u.i = "; u.i
print "u.s = "; u.s
print "u.b = "; u.b

u.i=2400

print "After u.i=2400:"
print "u.i = "; u.i
print "u.s = "; u.s
print "u.b = "; u.b

print "Addresses of the union members:"
print "u.i = "; @u.i
print "u.s = "; @u.s
print "u.b = "; @u.b

SLEEP
url]http://fbide.sourceforge.net/[/url]
Reply
#5
Now I'm understand the union a bit. But, from the example it does not show any useful of it since its not representing elements of any data type.

If it does, the following example would give some advantages:

Code:
Union HTMLColor As Integer
    B As Byte
    G As Byte
    R As Byte
End Union

Dim HC As HTMLColor

HC = &HEFF3F6

print "HC.B = ";HC.B
print "HC.G = ";HC.G
print "HC.R = ";HC.R

SLEEP

If a union is a structure within a data type, is has the usefulness for being existed.


At first I tought Union is fraction of an UDT where you built different UDT with different combination of Unions structure yet it use the same representation of each element. Such as the following:

Code:
Union StrProperties
    caption As String * MAX_TEXT
Union WinProperties

Union NumProperties
    top     As Integer = 0
    left    As Integer = 0
    height  As Integer = 320
    width   As Integer = 240
End Union

Union WinMethods
    Close     As SUB
    ShowModal As SUB()
    Fillrect  As SUB(x,y,x2,y2,clr)
    Paint     As SUB(x,y,clr)
    Line      As SUB(x,y,x2,y2,clr)
    Draw      As SUB(x,y,BMP)
    Visible   As FUNCTION() As Boolean
    Center    As SUB
End Union

Union WinEvents
    OnClick   As Long PTR
    OnPaint   As Long PTR
    OnClose   As Long PTR
    OnResize  As Long PTR
End Union

'----------------------------------

Type FBWindow
    sProp  As StrProperties

    nProp  AS NumProperties
    Method As WinMethods
    Event  As WinEvents
End Type

Type FBRect
    Prop As NumProperties
End Type

'----------------------------------

DIM MyRECT As FBRect
    
    With MyRECT
       .width  = 300
       .height = 400
    End With
    

DIM MyForm As FBWindow

    With MyForm
       .Caption = "HB GUI Window"
       .Center
       .OnResize = @SizeWin
       .ShowWindow
    End With

End 'program

Sub SizeWin
   ' code
End Sub
Reply
#6
The thing is useful, among other things, and as mentioned, to save memory. It is used for example in the structure which is used to give attributes to a grammar using flex/bison, when symbols can have information attached. As this information can be of different types, you could have used a normal type structure, but that wastes memory as only one field would be used. Using a union, you get the same functionality but you take only the memory which is needed to store the biggest element.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#7
ahh...okay, I got it. Its useful for making Variant data type?

Code:
Union Variant
   lpz   As String*4
   int   As Integer
   char  As Byte
   word  As Short
   dword As Long
   float As Single
   dbl   As Double
End Union

DIM vVar As Variant
    vVar.dword = 123/3

Print vVar.float
Print vVar.dbl
Print vVar.lpz
Print vVar.int

sleep

But the print-out suggesting it far from knowing how to use it and where it's best to use for.
Reply
#8
It is also useful for accessing the same data in multiple ways. An example is accessing an IP address as either an 32 bit integer or as four seperate bytes (and a string, just because you can)...
[syntax="freebasic"]
type ByteType
Byte1 as ubyte
Byte2 as ubyte
Byte3 as ubyte
Byte4 as ubyte
end type

union IP_Address
Value as uinteger
Bytes as ByteType
Text as string * 4
end union

dim addr as IP_Address

addr.Value = &HC0A80122

print "IP as bytes: "; addr.Bytes.Byte4; ".";
print addr.Bytes.Byte3; ".";
print addr.Bytes.Byte2; ".";
print addr.Bytes.Byte1
print "IP as integer: "; addr.Value
print "IP as string: "; addr.Text

sleep
[/syntax]
would output something like
Code:
IP as bytes:     192.168.1.34
IP as integer:   3232235810
IP as string:    "☺¿└
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)