Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Union in VB

Status
Not open for further replies.

SeanGriffin

Programmer
Feb 23, 2001
99
US
Does anyone Know how to make a union in VB?

 
Example

"SELECT COUNT(Polisnr) AS xx1, " _
& "SUM(Premie*Termijnbet) AS yy1, " _
& "SUM(inkomen*termijnbet) AS zz1 " _
& "FROM Polissen, Branche WHERE Polissen.Branche=Branche.Branche
AND Branche.Soort='700' " _
& "UNION SELECT COUNT(Polisnr) AS xx1, " _
& "SUM(Premie*Termijnbet) AS yy1, " _
& "SUM(inkomen*termijnbet) AS zz1 " _
& "FROM pakketPolis, Branche WHERE
pakketPolis.Branche=Branche.Branche AND Branche.Soort='700'"
Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
I was thinking more along the lines of a C union where there are different variable types shareing the same memory space.
 
You can do it, but it's a hack.

What you do is create a fixed-length string the size of the largest data element you plan to hold in the union. You then use the CopyMemory function to copy into/outof that variable.
[tt]
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
[/tt]

Example:
[tt]
Dim sStore as string * 10
Dim lValue as long

lValue = 1234
CopyMemory(byval sStore, lValue, len(lvalue))
lValue = 0
debug.print lvalue
CopyMemory(lValue, byval sStore, len(lvalue))
debug.print lvalue
[/tt]

The same can be done with other datatypes, user-defined structures, etc. But not classes or other objects.

The most important thing here is to keep an eye on the length of things. Be sure to allocate enough space to store everything, and make sure that the length parameter to the CopyMemory call is correct. If you forget, you get a visit from Dr. Watson.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top