Pages

Advertisement

Friday, July 13, 2007

Use Interop Code and Overlap Fields with the Union Construct in VB .NET

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, such as 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.

Although 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.

Implementing Union

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 interop—that 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

<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 such as Object and String with value types like integer and char.

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

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

The other values of LayoutKind are Auto, Explicit, and Sequential:


Refer to Microsoft's integrated help for more examples of StructLayout, LayoutKind, and FieldOffset.

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.

No comments: