|
-
Oct 20th, 2000, 02:13 AM
#1
Thread Starter
Fanatic Member
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!)
-
Oct 20th, 2000, 07:18 AM
#2
what are you trying to do? there are situations, where you can fake unions using LSet.
best regards
Sascha
-
Oct 20th, 2000, 11:36 AM
#3
Thread Starter
Fanatic Member
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!)
-
Oct 20th, 2000, 02:09 PM
#4
Unfortunately, VB doesn't support Union.
-
Oct 20th, 2000, 09:34 PM
#5
Thread Starter
Fanatic Member
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!)
-
Oct 20th, 2000, 10:10 PM
#6
I believe that Union is something they added into VB 7 but until it comes out I think you're SOL.
-
Oct 22nd, 2000, 10:43 AM
#7
Frenzied Member
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.
-
Oct 22nd, 2000, 07:02 PM
#8
Thread Starter
Fanatic Member
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!)
-
Oct 23rd, 2000, 04:55 PM
#9
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
-
Oct 23rd, 2000, 06:29 PM
#10
Hyperactive Member
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
-
Oct 23rd, 2000, 06:33 PM
#11
Hyperactive Member
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
-
Oct 23rd, 2000, 07:46 PM
#12
Thread Starter
Fanatic Member
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!)
-
Oct 23rd, 2000, 08:14 PM
#13
Hyperactive Member
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
-
Oct 24th, 2000, 12:05 AM
#14
Thread Starter
Fanatic Member
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!)
-
Oct 24th, 2000, 04:24 AM
#15
Hyperactive Member
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 
-
Oct 24th, 2000, 04:02 PM
#16
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|