Previous | Index | Next 

[HOWTO] Convert from OLE types to .NET types

VB Migration Partner automatically takes care of converting OLE types – such as StdPicture and StdFont objects – into the corresponding .NET types. In some cases, however, you might need to perform this conversion manually. This step can be necessary, for example, if you receive an OLE type from an external COM component or if you are to pass a .NET type to an external COM component.

VB Migration Partner’s support library contains all the methods that you need for these tasks, namely:

Converts from a 32-bit OLE color into a System.Drawing.Color object

Converts a System.Drawing.Color object into a 32-bit OLE color.

Converts from a StdPicture object to a System.Drawing.Image object.

Convents a System.Drawing.Image object into a StdPicture object. Also supports the conversion of Icon and Cursor objects.

Converts from a StdFont object into a System.Drawing.Font object

Converts a System.Drawing.Font object into a StdFont object.

These methods are defined in the VBMigrationPartner_Support module as “passing methods” – that is, they just return their argument, without performing any conversion – therefore you can use these names in the original VB6 code.

For example, let’s assume that ColorPicker is a COM component that displays a color picker dialog, that you don’t plan to convert it to VB.NET (possibly because you don’t have the source code), and that this component exposes an InitColor property that takes a 32-bit OLE color. Next, consider the following VB6 code

        Dim picker As New ColorPicker
        picker.InitColor = Text1.ForeColor

The problem with this code is that the Text1.ForeColor property returns a System.Drawing.Color object, therefore the assignment throws an exception.

The simplest way to work around this problem is adding the VBMigrationPartner_Support module to the VB6 project and then wrapping the expression to the right of the = symbol in a ToOleColor6 method:

        picker.InitColor = ToOleColor6(Text1.ForeColor)

This code works well under VB6 – because ToOleColor6 is a do-nothing method – and works under VB.NET as well, because VB Migration Partner gets rid of the VBMigrationPartner_Support module and makes ToOleColor6 reference the method defined in CodeArchitects.VBLibrary.dll.


Previous | Index | Next