Setting default Object properties to IColor is problematic


Summary
Using the Visual Basic Upgrade Wizard in Visual Studio to auto-translate Visual Basic 6 (VB6) code to Visual Basic .NET (VB .NET) is not 100 percent automated. This topic shows ArcGIS developers how to manually correct code where setting an unspecified default property of an object to IColor in VB6 does not work after the auto-translation.

About setting default Object properties to IColor

The following is an example VB .NET error:
  • Error 1 Value of type 'System.Drawing.Color' cannot be converted to 'Integer'.
The following is the cause of the VB .NET error:
  • In VB6, objects had a default property that could be used as a shortcut in the code. For example, the default property of a TextBox control was the Text property. You could type TextBox1 = "Hello" instead of TextBox1.Text = "Hello." In VB .NET, default properties are no longer supported. All property references must be fully qualified. During the upgrade, default properties for early-bound objects are resolved to the property name; however, late-bound objects cannot be resolved because it is impossible to determine a default property without knowing what the object is. Sometimes the VB6 to VB .NET upgrade wizard overly complicates the code. Evaluate the argument and correct the code.
The following shows the original VB6 code example:
Private Sub ChangeTextboxColor(aTextbox As TextBox, colNew As IColor)
aTextbox.BackColor = colNew.RGB
End Sub
The following shows the initial auto-translated code example:
[VB.NET]
'Initial auto-translated VB.NET conversion.

Private Sub ChangeTextboxColor(ByRef aTextBox As TextBox, ByVal colNew As ESRI.ArcGIS.Display.IColor)
    aTextBox.BackColor = System.Drawing.ColorTranslator.FromOle(CInt(System.Drawing.ColorTranslator.FromOle(System.Convert.ToInt32(colNew.RGB))))
End Sub
The following shows the correct VB .NET translation code example:
[VB.NET]
'Correct VB .NET translation.

Private Sub ChangeTextboxColor(ByRef aTextBox As TextBox, ByVal colNew As ESRI.ArcGIS.Display.IColor)
    aTextBox.BackColor = System.Drawing.ColorTranslator.FromOle(colNew.RGB)
End Sub


See Also:

Migrating from VB6 to VB.NET
General steps for migrating from VB6 to VB.NET




Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine