Bad Constants

Bad Constants

In Visual Basic 6, you can create a set of named constants, known as an enumeration. Each member of the enumeration is assigned an integer value; in code, the member name evaluates to its assigned integer. Because enumerations in Visual Basic 6 represent integer values instead of distinct types, the process can verify only their underlying values. Unfortunately, this arrangement allows developers to use constants intended for different purposes interchangeably and also to use simple integers (representing the underlying values) in place of the constants. Visual Basic .NET is more strict and requires the correct enumeration constants because enumerations are now actual types, not just collections of friendly names for integers. The most common example of the use of bad constants is in relation to the Screen.MousePointer property. Constants such as vbNormal are frequently used in place of vbDefault (a proper constant for the MousePointer property). It is also fairly common for developers to use the underlying values for constants. The following example from Visual Basic 6 demonstrates three equivalent statements that work just fine:

' Correct MousePointer constant Screen.MousePointer = vbDefault      ' Incorrect constant, same underlying value Screen.MousePointer = vbNormal      ' The same underlying value Screen.MousePointer = 0

When you upgrade your project, however, the Upgrade Wizard will not be able to upgrade the vbNormal constant because it is not defined for the MousePointer and you will be left with an upgrade warning and a compile error. On the other hand, the Upgrade Wizard can work with an underlying constant, if used, and can translate it to a proper constant in Visual Basic .NET. The following results from the Upgrade Wizard illustrate this scenario:

' Correct MousePointer constant 'UPGRADE_WARNING: VB.Screen property Screen.MousePointer has a  'new behavior. System.Windows.Forms.Cursor.Current = _    System.Windows.Forms.Cursors.Default ' Incorrect constant, same underlying value 'UPGRADE_ISSUE: Unable to determine which constant to upgrade  'vbNormal to. 'UPGRADE_ISSUE: VB.Screen property Screen.MousePointer does not  'support custom mousepointers. 'UPGRADE_WARNING: VB.Screen property Screen.MousePointer has a  'new behavior. System.Windows.Forms.Cursor.Current = vbNormal ' The same underlying value 'UPGRADE_WARNING: VB.Screen property Screen.MousePointer has a  'new behavior. System.Windows.Forms.Cursor.Current = _    System.Windows.Forms.Cursors.Default

If you have used an incorrect constant, you will need to fix it on your own. The Upgrade Wizard cannot help you, other than highlighting the problem in the upgrade report. Thankfully, the solution is very simple, thanks to IntelliSense. Simply delete the offending constant, and you get an IntelliSense menu listing the proper constant options. Pick the appropriate constant, and you are on your way.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net