Default Properties

Default Properties

Default properties are no longer supported in the common language runtime. If the Upgrade Wizard can determine a default property, it will properly modify your code to state the property explicitly in Visual Basic .NET. If it cannot determine the property being referenced (in the case of late binding), the wizard will leave your code as is and insert a warning highlighting the issue for you to resolve. In those cases, the offending line of code will be preserved through the upgrade and tagged to bring it to your attention. While it is possible (and often more desirable) to go back to your original code and modify it, there are definitely cases in which it is not possible to make those changes anywhere but in the upgraded application.

The following code samples demonstrate the differences in upgrade results when default properties are accessed using both late-bound and early-bound objects. The code is contained in the DefaultProperties sample included on the companion CD.

Private Sub CopyButton_Click()    EarlyBoundCopy Text1, Text2    LateBoundCopy Text1, Text3 End Sub ' This method's parameters are explicitly defined as TextBoxes Private Sub EarlyBoundCopy(sourceCtrl As TextBox, destCtrl As TextBox)    destCtrl.Text = sourceCtrl End Sub ' This method's parameters are variants (the default type) Private Sub LateBoundCopy(sourceCtrl, destCtrl)    destCtrl.Text = sourceCtrl End Sub

Although this code runs just fine in Visual Basic 6, the Upgrade Wizard will insert two run-time warnings in your code. This type of warning means that the code will compile after upgrading but will generate an exception at run time. Specifically, the LateBoundCopy method will generate an InvalidCast Exception. The exception says the following: Cast from type TextBox to type String is not valid.

Here is the code the Upgrade Wizard generates:

Private Sub CopyButton_Click(ByVal eventSender As System.Object, _    ByVal eventArgs As System.EventArgs) Handles CopyButton.Click    EarlyBoundCopy(Text1, Text2)    LateBoundCopy(Text1, Text3) End Sub ' This method's parameters are explicitly defined as TextBoxes Private Sub EarlyBoundCopy(ByRef sourceCtrl As  _    System.Windows.Forms.TextBox, ByRef destCtrl As _    System.Windows.Forms.TextBox)    destCtrl.Text = sourceCtrl.Text End Sub ' This method's parameters are variants (the default type) Private Sub LateBoundCopy(ByRef sourceCtrl As Object, _    ByRef destCtrl As Object)    'UPGRADE_WARNING: Couldn't resolve default property of object     'destCtrl.Text.    'UPGRADE_WARNING: Couldn't resolve default property of object     'sourceCtrl.    destCtrl.Text = sourceCtrl End Sub

When you encounter these kinds of problems, you will need to modify your code. Two options are available. First, you can change the definition of the variable to be strongly typed in Visual Basic 6 and run your application through the Upgrade Wizard again. Your other option is to explicitly specify the desired property in Visual Basic .NET. This last option results in the LateBoundCopy method looking like this:

Private Sub LateBoundCopy(ByRef sourceCtrl As Object, _    ByRef destCtrl As Object)    destCtrl.Text = sourceCtrl.Text End Sub

note

The second option involves modifying your code to explicitly use the property you want to read or modify. Notice that this is exactly what the Upgrade Wizard does for you when it knows what type it is dealing with. This example illustrates the advantage of always using early binding. The Upgrade Wizard will know what the default properties are and will take care of the work for you. Using late binding puts you in the position of going through all of the upgrade warnings and manually entering the desired properties.

The fact that you can upgrade an application that contains these kinds of run-time problems should lead you to two conclusions. First, you need to pay close attention to the issues raised by the Upgrade Wizard. Second, you must test your upgraded application thoroughly to make sure that the original functionality has been maintained. The next section examines how using default properties can also lead to additional and more subtle run-time differences.



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