Common issues when migrating from Visual Basic 6.0 to Visual Basic .NET


This document was published with and applies to ArcGIS 9.3.
A 9.2 version also exists.

In this topic


Dealing with issues before upgrading

The following outlines how to deal with issues found by the Visual Basic (VB) 6.0 code advisor add-in when accessing your VB 6.0 code before upgrading:

Dealing with issues after upgrading

The following outlines how to deal with issues found by the Visual Basic Upgrade wizard after upgrading. The Visual Basic Upgrade wizard cannot automate 100 percent of the necessary code changes when migrating from VB 6.0. In some cases, no direct equivalent for the VB 6.0 code may be available. In other cases, you may need to make manual changes to take advantage of the new features available in Visual Basic .NET (VB.NET).
 
The upgrade wizard also adds comments in your code to highlight the issues listed in the upgrade report. Press Ctrl+Alt+K to open the Task List and double-click an issue to go to that line of code. By default, only the upgrade error issues are listed in the Task List. To also display all upgrade warning issues (those that may not actually cause a compilation error), click Tools, Options, Environment, then Task List and add UPGRADE_WARNING to the displayed token list.
 
Before you go through the upgrade report of your new .NET project, review the following list of commonly encountered issues:
 

[VB.NET]
Private ReadOnly Property ICommand_Bitmap() As Integer Implements ICommand.Bitmap
Other examples of where you can encounter this error are the members ITool.Cursor, IToolControl.hWnd, and IDockableWindowDef.hWnd.
 
For example, in the custom VertexLineSymbol developer sample, there is a class called IVertexLineSymbol that defines a custom interface. During the upgrade process, the wizard created both the class and the hidden default interface for the class, resulting in a class called IVertexLineSymbol and an interface called _IVertexLineSymbol. The _IVertexLineSymbol interface will be implemented by both the IVertexLineSymbol interface class and the actual custom symbol class VertexLineSymbol (try the conversion to understand more about this issue).
 
As VB.NET provides the interface keyword, you can remove the class interface, leaving just the interface definition. You may want to rename the interface by removing the underscore prefix (do not forget to update any other uses of the interface throughout your code if you do rename the interface). In the example of the VertexLineSymbol developer sample, remove the IVertexLineSymbol class and leave the _IVertexLineSymbol declaration. Rename the interface to IVertexLineSymbol and change any other references in the code—including the Implements statement in the VertexLineSymbol class.
The following is not an essential step, but it will help reduce complexity in your project and cut a few unnecessary calls at runtime.
 
The upgrade wizard highlights lines of code where types are late bound when an implicit cast is made, as it assumes a default property is being used to access the correct type—for example, creating a new AlgorithmicColorRamp and setting its FromColor property from a red, green, and blue (RGB) color as shown in the following code example:

[VB.NET]
Dim pAlgoColorRamp As IAlgorithmicColorRamp
Set pAlgoColorRamp = New AlgorithmicColorRamp

Dim pRGBColor As IRgbColor
Set pRGBColor = New RgbColor
pAlgoColorRamp.FromColor = pRGBColor ' FromColor property requires an IColor reference.
These lines of code caused an upgrade warning indicating the default property of the object pRGBColor cannot be resolved. However, the code is correct and there is no default property being used as an implicit cast is made to the IColor interface of the IRGBColor reference pRGBColor. However, as the FromColor property requires an IColor reference and pRGBColor is an IRGBColor reference, the wizard assumes a default member is being used to return the correct type—it does not know that an implicit cast is made between these interfaces in VB 6.0. You can ignore these cases or just add explicit casting if required, for example, if Option Strict is turned on. See the following code example:
 

[VB.NET]
Dim pAlgoColorRamp As ESRI.ArcGIS.Display.IAlgorithmicColorRamp = New ESRI.ArcGIS.Display.AlgorithmicColorRamp
Dim pRGBColor As ESRI.ArcGIS.Display.IRgbColor = New ESRI.ArcGIS.Display.RgbColor
pAlgoColorRamp.FromColor = DirectCast(pRGBColor, ESRI.ArcGIS.Display.IColor)

Required changes after upgrading

The following information shows how to deal with issues—for ArcObjects code in .NET—that may not be highlighted by the upgrade wizard related specifically to ArcObjects API or Component Object Model (COM) interop:

Other common issues after upgrading

The following information discusses issues you may encounter when migrating code from VB 6.0 to .NET that are not highlighted by the upgrade wizard and are not specifically related to ArcObjects:
 

[VB.NET]
' Declaration for the handle clean-up function.
Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As IntPtr) As Boolean
...
Dim bm As New System.Drawing.Bitmap(frmResources.DefInstance.Picture3.Image)
Dim hBitmap As IntPtr = bm.GetHbitmap()
Return hBitmap.ToInt32()
DeleteObject(hBitmap

[VB.NET]
' Declare the helper class for IMapEvents as WithEvents.
Private WithEvents m_pMapEvents As ESRI.ArcGIS.Carto.MapEvents
...

Public Sub SomeSetupFunction()
    ' Initialize the MapEvent variable.
    m_MapEvents = doc.FocusMap
End Sub

Private Sub m_MapEvents_FeatureClassChanged(ByVal oldClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, _
                                            ByVal newClass As ESRI.ArcGIS.Geodatabase.IFeatureClass) Handles m_MapEvents.FeatureClassChanged
    ' Listen to the FeatureClassChanged event of IMapEvents.
End Sub
However, when you set the m_pMapEvents2 variable in .NET, an exception is raised, indicating that the specified cast is invalid.
If you do not have any exception handling, you may not be aware that this situation is occurring.
To solve this issue, you can use the AddHandler method of events sinking rather than using the WithEvents keyword—this means that instead of declaring module-level variables WithEvents, you will instead declare delegates and wire these to the appropriate events. In the previous example of the IMapEvents2 event interface, this can be changed to use code similar to the following:

[VB.NET]
' Declare a delegate for the FeatureClassChanged event.
Private dFeatClsChangedE As ESRI.ArcGIS.Carto.IMapEvents_FeatureClassChangedEventHandler
...

Public Sub SomeSetupFunction()
    ' Initialize the MapEvent variable.
    Dim mapEvents As ESRI.ArcGIS.Carto.IMapEvents_Event = CType(doc.FocusMap, ESRI.ArcGIS.Carto.IMapEvents_Event)
    ' Initialize the delegate to point to a function where you respond to the event being raised.
    dFeatClsChangedE = New ESRI.ArcGIS.Carto.IMapEvents_FeatureClassChangedEventHandler(AddressOf OnFeatureClassChangedFunction)
    ' Wire the delegate to the FeatureClassChanged event of the mapEvents variable.
    AddHandler mapEvents.FeatureClassChanged, dFeatClsChangedE
End Sub

Private Sub OnFeatureClassChangedFunction(ByVal oldClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, _
                                          ByVal newClass As ESRI.ArcGIS.Geodatabase.IFeatureClass)
    ' Listen to the FeatureClassChanged event of IMapEvents.
End Sub
For more information on using AddHandler, see the following topics in the Programming with .NET section in this help system:
 
Using AddHandler requires more coding, as this is less automated in the Visual Studio .NET IDE. However, it has the advantages of being more flexible and more directly translatable to equivalent C# code.
This requires changing the default security of the .NET framework. Always check with your system administrator to ensure this does not violate any local or domain security policies.
The following are the basic permissions that you will need to run interop code from a network path:


See Also:

Interoperating with COM
How to register .NET components with COM
Working with the ArcGIS base classes