# Visual Basic > Visual Basic 6 and Earlier >  [RESOLVED] Emulating 'union' by defining types

## kiymik

Hi, a subject discussing time to time, and again...
Since there is no equivalent to 'union' as in c I tried to define two types, one is array of bytes other is a list of variables, defined Var1 as Type1 and Var2 as Type2. Their sizes are same. I don't need to share same memory space, if only the bytewise datas are equal it will be ok. So I expected to use Var1 = Var2 and get same byte list in two typed variables but it didn't work, said their types are different so cannot execute this equality. There may be a way to do this in some api, if you know some way will be appreciated. Thanks. Sabri.

----------


## VanGoghGaming

The best you could do is CopyMemory from Var2 into Var1.

----------


## kiymik

Ok thanks. That was something I've found and now working on and seems it will do the job. It was a project communicating with a microcontroller whose program is written in c, variables are stored in little endian format, so it already fits VB structure making life easier (of course may differ in float type, will see).

----------


## Schmidt

*LSet* UDTVarOfType1 = UDTVarOfType2

...should do the Job as well (assuming that the "amount of bytes" behind the two TypeDefintions is the same).

Olaf

----------


## The trick

```
Option Explicit

Private Type t1
    d As Double
End Type

Private Type t2
    c As Currency
End Type

Private Sub Form_Load()
    Dim p As t1, q As t2
    
    q.c = 123
    
    LSet p = q
    
End Sub
```

----------


## The trick

> *Let* UDTVarOfType1 = UDTVarOfType2


LSet not Let

----------


## Schmidt

> LSet not Let


Right, ... thanks (it's corrected now in my prior post)

Olaf

----------


## kiymik

Thanks, I will try at least to learn some more. Will take care of 'lset' also  :Smilie:  I was thinking 'lset' works only for strings.

----------


## wqweto

> Hi, a subject discussing time to time, and again...
> Since there is no equivalent to 'union' as in c I tried to define two types, one is array of bytes other is a list of variables, defined Var1 as Type1 and Var2 as Type2. Their sizes are same. I don't need to share same memory space, if only the bytewise datas are equal it will be ok. So I expected to use Var1 = Var2 and get same byte list in two typed variables but it didn't work, said their types are different so cannot execute this equality. There may be a way to do this in some api, if you know some way will be appreciated. Thanks. Sabri.


Can you post exact Type declarations you are talking about -- the byte-array in Type1 and the list of variables in Type2

In TwinBASIC this works:



```
'--- Module1.bas
Option Explicit

Private Type Type1
    Item(0 To 31)   As Byte
End Type

Private Type Type2
    Var1            As Long
    Var2            As Long
    Var3            As Long
    Var4            As Long
    Var5            As Currency
    Var6            As Currency
End Type

Public Sub MyTest()
    Dim uBuffer     As Type1
    
    ReadBuffer uBuffer
    ProcessBuffer VarPtr(uBuffer)   '--- on procedure params TB allow raw LongPtr instead of address of UDT 
End Sub

Private Sub ReadBuffer(uBuffer As Type1)
    '...
End Sub

Private Sub ProcessBuffer(uData As Type2)
    Debug.Print uData.Var1
End Sub
```

You can do almost the same in VB6 using CallWindowProc or DispInvoke API functions.

cheers,
</wqw>

----------

