DEV341 Visual Basic: Migrating and Upgrading Lessons Learned Jay Roxe Product Manager Microsoft Agenda Migration Strategy And Pitfalls VB .NET Language Aids VB6 Code Advisor Migration Wizard Amplifying the Application Not Everything Gets Migrated Inventory and evaluate applications Education: Get some VB .NET Experience Target a few small applications Pick Carefully! Evaluate the apps first Migrate one piece at a time Horizontal Slice: UI or Middle Tier Vertical Slice: Bank forms New Application Development Tools help migration VB6 Application Prepare for upgrade VB6 VB6 VB6 VB6 VB6 Code Advisor VB6 VB.NET Upgrade and fix issues Migration Wizard Enhance with new features Micro Issues are 88% Simple to fix. Create “Noise” Five issues represent 88% of all upgrade issues Default properties 52% Property/method not upgraded 13% Property/method different behavior 12% Module methods of COM objects 7% Null/IsNull 4% Strongly Typed Variables Visual Basic 6.0 code Dim myTextBox As TextBox myTextBox = "Hey There" After upgrade Dim myTextBox As System.Windows.Forms.TextBox myTextBox.Text = "Hey There" Result: works perfectly Latebound Variables Visual Basic 6.0 code Dim myTextBox myTextBox = "Hey There" After upgrade Dim myTextBox As Object 'UPGRADE_WARNING: Couldn't resolve default property of object myTextBox. myTextBox = "Hey There" Result: Runtime error Choice Of Solutions Solution 1: Prepare in Visual Basic 6.0 Dim myTextBox As TextBox myTextBox = "Hey There" Solution 2: Fix after upgrading Dim myTextBox As Object 'UPGRADE_WARNING: Couldn't resolve default property of object myTextBox. .Text = "Hey There" myTextBox.Text Macro Issues ActiveX Documents DHTML Pages UserControls. Leave in VB6, Navigate to VB.NET StrPtr, ObjPtr, VarPtr Graphics Memory pinning Use GDI+ Deterministic finalization Dispose Use GC methods RDO/DAO data binding Use ADO, ADO.NET Transactions System.EnterpriseServices GoSub Procedures Agenda Migration Strategy And Pitfalls VB .NET Language Aids VB6 Code Advisor Migration Wizard Amplifying the Application Using COM From .NET IUnknown Common Language Runtime ComServer Object IFoo Runtime Callable Wrapper Reference Counted Client Best Practice Use Primary Interop Assemblies Produced by COM API owner May be modified for correctness or managed client friendliness Recognized by Visual Studio.NET Ensure consistent identity for Interop types Visual Studio ships with PIAs C:\Program Files\Microsoft.NET\Primary Interop Assemblies Office 2003 PIAs also available http://msdn.microsoft.com/library/default.asp?url=/downlo ads/list/office.asp Exposing Managed APIs to COM Many unmanaged clients/hosts exist Internet Explorer Office Windows shell Every managed object is a COM object Has a class factory Implements IUnknown, IDispatch Type library can be generated using tlbexp.exe (Type Library Exporter) CCW (COM Callable Wrapper) takes care of all this during runtime .NET from COM IUnknown COM Client Common Language Runtime IDispatch IFoo COM Callable Wrapper IFoo Client Server Problems Not all the features of managed objects are exposable to COM Static (shared) methods not exposed Parameterized constructors not exposed Overloaded methods are renamed COM Visibility Types are visible to COM by default; so what? Are they usable from COM by default? Maybe not! .NET Framework Examples System.Math contains only static methods System.Collections.BitArray does not have a default constructor COM Interop VisualBasic.dll Collection Constants (vbCancel, vbCrLf… ControlChars (CrLf, Tab,… DateAndTime (DateAdd… ErrObject FileSystem Information (IsDate, IsDBNull, … VBMath VisualBasic.Compatibility.dll Caution: Functions in the Visual Basic 6.0 Compatibility library are provided only for use by the upgrading tools. Although it is possible to use this library when writing new code, there is no guarantee that it will be supported in future versions of Visual Basic. Class DirListBoxEx Inherits Microsoft.VisualBasic.Compatibility.VB6.DirListBox End Class Agenda Migration Strategy And Pitfalls VB .NET Language Aids VB6 Code Advisor Migration Wizard Amplifying the Application Prepare VB6 code Strongly type variables Null Propagation Non zero lower bound arrays Replace values with Enums Incompatible properties Module methods “Code Advisor” a.k.a “FixIt” Configuration HTML summary Extending the “Code Advisor” Dim prpObj As Property 'FIXIT: Declare 'vTmp' with an early-bound data type Dim vTmp As Variant 'FIXIT: Declare 'vNew' with an early-bound data type Dim vNew As Variant Dim frmProp As New frmProperty FixIT90210ae-R1672-R1B8ZE FixIT90210ae-R1672-R1B8ZE Preparing VB 6 Projects Visual Basic 6.0 Code Advisor Issues Identified by the Code Advisor 1.) <'# condition'> not upgraded reliably to Visual Basic .NET 11.) The use of <'enumeration'> is not valid for the property being assigned 2.) <'item'> project item type is not supported in Visual Basic .NET and will not be upgraded 12.) There is no Line control in Visual Basic .NET 3.) <'object'> not upgraded to Visual Basic .NET by the Upgrade Wizard 13.) There is no Shape control in Visual Basic .NET 4.) <'property / method / event'> has no Visual Basic .NET equivalent and will not be upgraded 14.) UpDown controls are not upgraded to Visual Basic .NET by the Upgrade Wizard 5.) As Any is not supported in Visual Basic .NET. Use a specific type 15.) Use Option Explicit to avoid implicitly creating variables of type Variant 6.) Changing ScaleMode at run-time is not supported 16.) <'property'> is not a property of the generic <'object'> object in Visual Basic .NET. 7.) Replace DAO Data Binding with ADO 17.) Whenever possible replace ActiveForm or ActiveControl with an early-bound variable 8.) Replace <'variant'> function with <'string'> function 18.) Declare <'variable'> with an early-bound data type 9.) Return has new meaning in Visual Basic 19) Keyword <'keyword'> not supported in VB6 Code Advisor Agenda Migration Strategy And Pitfalls VB .NET Language Aids VB6 Code Advisor Migration Wizard Amplifying the Application Before Upgrade Wizard Must be able to compile VB6 app Must have design time license files Must have all referenced components (required for Interop assemblies) Should remove unused references, particularly for ActiveX controls Upgrade Wizard EXE & DLL Copies project Creates reports Links to Help Four Levels Issue ToDo Warning Note No Automatic Upgrade Requires Finishing Possible Behavior Change Informational Upgrade Wizard Form Layout ActiveX Controls ADO Data Binding RES files Code Syntax Changes COM Libraries Behavior warnings Visual Basic .NET Migration Wizard Agenda Migration Strategy And Pitfalls VB .NET Language Aids VB6 Code Advisor Migration Wizard Amplifying the Application Take Advantage of .NET Replace ADO with ADO .NET Replace ActiveX controls Web-enable Mobile front end Reflection Web services Adding an ASP.NET Front End Looking Forward: VB 2005 Upgrade MTS and COM+ projects • Upgrade the following controls from VB6 to Windows Forms controls: WebBrowser, Toolbar, ImageList, TreeView, ListView, OleContainer, RichTextEdit, ProgressBar, StatusBar, CommonDialog, MaskedEdit • Upgrade code that uses string-based-keys in forms control collections • Upgrade code that changes the key value in the KeyPress event • Upgrade to the BackgroundImageLayout property • Upgrade the VB6 App object to the Whidbey My object • Upgrade Unload Mode Parameter of QueryUnload event to ClosingReason • Upgrade forms to use the new Partial Type format • Upgrade default instance to use Whidbey's support for default instances • Upgrade the forms collection to the Whidbey forms collection • Upgrade VB6 Printers collection object to the My.Computer.Printers object • Upgrade VB6 clipboard object to the My.Computer.Clipboard • Upgrade LoadResString, LoadResData, LoadResPicture to use My.Resources Attend a free chat or web cast http://www.microsoft.com/communities/chats/default.mspx http://www.microsoft.com/usa/webcasts/default.asp List of newsgroups http://communities2.microsoft.com/ communities/newsgroups/en-us/default.aspx MS Community Sites http://www.microsoft.com/communities/default.mspx Locate Local User Groups http://www.microsoft.com/communities/usergroups/default.mspx Community sites http://www.microsoft.com/communities/related/default.mspx Please fill out a session evaluation on CommNet Q1: Overall satisfaction with the session Q2: Usefulness of the information Q3: Presenter’s knowledge of the subject Q4: Presenter’s presentation skills Q5: Effectiveness of the presentation © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.