http://www.developer.com/net/vb/article.php/3602621/Use-Interop-Code-and-Overlap-Fields-with-the-Union-Construct-in-VBNET.htm
There are few idioms and constructs that Visual Basic .NET cannot touch. Although not especially good at pointers and addresses, VB.NET and C# enable a developer to emulate, contrive, or precisely reproduce almost everything else that even the most complex languages, like C++, offer. One such construct is the union. A union is like a structure, but it permits all its fields to share the same starting address. Consider a union with character and integer fields. The union structure would be 32 bits; that is, the union would be the size of the largest element. Assign a value to the char (say, 65) and the union would assign the integer the same value. When accessed through the integer field, it would return the value 65. When accessed through the character field, it would return the value A. Change one field and the other changes as well. Peter Norton did some famous work with unions that resulted in the now ubiquitous undelete application. While the most common reason to use a union probably is to map more than one field to the same memory address, you are not limited only to this use. This article demonstrates how to implement the union construct in VB.NET. You'll know when you need it. The union construct commonly is used to map more than one cardinal field to the same address. After all, cardinal fields (or numbers) overlap nicely. However, the most common application in .NET likely will be for interopthat is, using code that was written in something other than .NET. Whenever you use interop code, you use the System.Runtime.InteropServices namespace. It contains a lot of interesting code, including the Marshal class. For the example in this article, you add an Imports statement, but you should explore this namespace further on your own. To implement the example union, take the following steps:
Listing 1 shows the results of this implementation. Listing 1: A Structure Named Union That Implements the Union Construct Listing 2 shows a simple console application that assigns the number 65 to the field i. When accessed through the field ch, the value is displayed as the character A. Listing 2: Using the Union Construct Demonstrated in Listing 1 The other values of LayoutKind are Auto, Explicit, and Sequential: Refer to Microsoft's integrated help for more examples of StructLayout, LayoutKind, and FieldOffset. Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his new book UML DeMystified from McGraw-Hill/Osborne. Paul is an architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at pkimmel@softconcepts.com. If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org. Copyright © 2006 by Paul T. Kimmel. All Rights Reserved.
Use Interop Code and Overlap Fields with the Union Construct in VB.NET
May 1, 2006
Implementing Union
<StructLayout(LayoutKind.Explicit)> _
Public Structure Union
<FieldOffset(0)> _
Public i As Integer
<FieldOffset(0)> _
Public ch As Char
End Structure
You are not limited to using integers and characters. You can use other types too, but you cannot mix reference types like Object and String with value types like integer and char.
Imports System.Runtime.InteropServices
Module Module1
Sub Main()
Dim u As Union
u.i = 65
Console.WriteLine(u.ch)
Console.ReadLine()
End Sub
End Module
<StructLayout(LayoutKind.Explicit)> _
Public Structure Union
<FieldOffset(0)> _
Public i As Integer
<FieldOffset(0)> _
Public ch As Char
End Structure
Union for VB.NET Programmers
VB.NET supports manufacturing even the advanced union idiom, which you previously found in languages like C and C++. Unions permit VB.NET programmers to overlap fields, which is useful when calling unmanaged code that returns a union.
About the Author