Results 1 to 16 of 16

Thread: Unions in VB??

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Question

    Is there an easy way of implementing Unions in VB? Like in C?

    It's a very handy way of manipulating data.

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  2. #2
    Guest
    what are you trying to do? there are situations, where you can fake unions using LSet.

    best regards

    Sascha

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    It's handy to be able to do this

    Code:
    Type Col
       Red As Byte
       Green As Byte
       Blue As Byte
       Unused As Byte
    End Type
    
    Union ColSplit
       Vb_Colour as Long
       Split as Col
    End Union
    So, because they take the same memory space passing a colour value to ColSplit.Vb_Colour allows me to check the individual colours by checking the union without moving any data which is very fast. I cn also increas the value for red and have the lng colour effected without any conversions

    I'd like to do similar things for breaking up other data types like getting the high and low word from a long etc


    The ability to hold multiple types in the same memory space is very powerful, is there an API call or something that I can duplicate this> I suppose that the overhead would be the same as doing type conversions



    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Unfortunately, VB doesn't support Union.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Thumbs down

    No way to emulate it?

    No cunning plans that anyone's come up with?

    I suppose without pointers it's a bit hard to get the bits without writing a function for every thing you're trying to do.

    bit poor on the part on VB really


    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I believe that Union is something they added into VB 7 but until it comes out I think you're SOL.

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    This is how I would do it. Not as elegant, but it works.
    Code:
    Private Declare Sub CopyMemory Lib "KERNEL32" _
                        Alias "RtlMoveMemory" (hpvDest As Any, _
                                               hpvSource As Any, _
                                               ByVal cbCopy As Long)
    Private Type Col
                 Red    As Byte
                 Green  As Byte
                 Blue   As Byte
                 Unused As Byte
            End Type
    Dim Vb_Colour As Long
    Dim Split     As Col
    .
    .
    .
    CopyMemory(Split, Vb_Colour, LenB(Vb_Colour))
    NOTE: be sure to use the Long to get the length as VB will add at least 1 byte to the user declared data type. Otherwise, you may trash whatever follows the Long in memory.


  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Not a bad solution but this might be a bit slow if you wanted to loop through a screen of colours, as every colour would have to be copied in and out of memory rather than the original worked on.

    I'll give it a go, I think it's the only way to test the performance.
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  9. #9
    Guest

    same solution without API

    this works too, but it is still a memory copy:

    '**********************************************************

    Private Type ColSplit
    Red As Byte
    Green As Byte
    Blue As Byte
    Unused As Byte
    End Type

    Private Type ColJoin
    Color As Long
    End Type

    Private Sub Command1_Click()

    Dim s As ColSplit, j As ColJoin
    With s
    .Red = 255
    .Blue = 255
    .Green = 10
    LSet j = s
    Me.Print "VbColor=" & j.Color
    LSet s = j
    Me.Print "r=" & .Red & ", b=" & .Blue & ", g=" & .Green
    End With

    End Sub

    '**********************************************************

    best regards

    Sascha

  10. #10
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    no way to emulate

    To emulate this, you'd need to get a friendly C++ programmer to code it up. Not that hard I imagine...as long as all the types you might want to use are already known.

    To emulate it (well not really) dynamically, I use LSet.

    If you're not familiar with this highly useful keyword let me know.

    It is not that useful if your types hold object pointers but if they are simple types, then it is quite handy.

    Regards
    Paul Lewis

  11. #11
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    I should add..

    If you only have simple data types (as per your example), then LSet is safer than CopyMemory and LSet is ideally suited for exactly what you want to do. I use it for doing any Long to Integer to Byte conversions (saves all the calculations).

    Regards
    Paul Lewis

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    I haven't used LSet, I'll look it up in MSDN help when I get home.

    I might just code up a quick win32 dll with the functionality I want. I'm just thinking that the function overhead off the dll call may neutralise the whole point of doing this which is not necessarily to use unions somewhere but speed...

    Maybe I'll just go back and implement this in PowerBasic or C so I can pick the speed up with a union
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  13. #13
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411
    If you've never used it before then you have been missing out on something when dealing with UDT's...

    The simple example below does what you want. Lset acts as the Union at runtime. It is definitely NOT the same of course because the two variables holding the UDT have different memory addresses.

    But as far as emulation goes...

    Code:
    Option Explicit
    Private Type lng
      lng1 As Long
    End Type
    
    Private Type byt
      b1 As Byte
      b2 As Byte
      b3 As Byte
      b4 As Byte
    End Type
    
    Private Sub Form_Load()
      Dim myLong As lng
      Dim myByte As byt
      myLong.lng1 = 12345678
      LSet myByte = myLong
      
      Debug.Print myByte.b1, myByte.b2, myByte.b3, myByte.b4
      
    End Sub
    Paul Lewis

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Thumbs up

    Useful, Thanks

    What's LSet stand for?
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  15. #15
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Smile Never thought to try to find out :)

    Possibly "Left Set" because we are simply copying the contents of one UDT to another but are Left Aligning the members.

    Well, that's my guess

    Paul Lewis

  16. #16
    Guest
    I know that there is ways, to directly access memory by address in VB too (some heavy tricks involved), but I can't send you code right now. I found all that in a decent book 'Advanced Visual Basic', but at the book is in my office, I am far away to reproduce it myself so far and I am on holiday ....

    If you want I can post something next week, but probably it is wiser to buy the book anyway, it's definately the most advanced book about VB I've ever seen, I think it will take me a few years to truly understand all it covers.

    best regards

    Sascha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width