Example of initializing for any field of Union:
Code: Select all
type vtype
vtype as ushort
namelen as ubyte
name as string * 15
union
i8 as byte
i16 as short
i32 as long
flt as double
spt as any ptr
end union
declare constructor()
declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i8 as byte)
declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i16 as short)
declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i32 as long)
declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _flt as double)
declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _spt as any ptr)
end type
constructor vtype()
end constructor
constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i8 as byte)
print "call constructor for 'byte' field initialization in union"
vtype = _vtype
namelen = _namelen
name = _name
i8 = _i8
end constructor
constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i16 as short)
print "call constructor for 'short' field initialization in union"
vtype = _vtype
namelen = _namelen
name = _name
i16 = _i16
end constructor
constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i32 as long)
print "call constructor for 'long' field initialization in union"
vtype = _vtype
namelen = _namelen
name = _name
i32 = _i32
end constructor
constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _flt as double)
print "call constructor for 'double' field initialization in union"
vtype = _vtype
namelen = _namelen
name = _name
flt = _flt
end constructor
constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _spt as any ptr)
print "call constructor for 'any ptr' field initialization in union"
vtype = _vtype
namelen = _namelen
name = _name
spt = _spt
end constructor
dim shared as vtype vars(1 to 128) = { type(1, 1, "1", Cast(byte, 1)), _
type(2, 2, "2", Cast(short, 2)), _
type(3, 3, "3", Cast(long, 3)), _
type(4, 4, "4", Cast(double, 4)), _
type(5, 5, "5", @"5") }
sleep
'Cast' is used to override the implicit conversion of Union argument when there are multiple constructor candidates.