1 / 52

DEV230 Upgrading VB 6 Applications to Visual Basic .NET

DEV230 Upgrading VB 6 Applications to Visual Basic .NET. Keith Pleas Architect, Guided Design keithp@guideddesign.com. Overview. Language changes – code, object, data Classic Visual Basic “Ruby” forms to Windows Forms Upgrade Tools & Utilities The Upgrade Process - Examples.

sherlock
Download Presentation

DEV230 Upgrading VB 6 Applications to Visual Basic .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DEV230Upgrading VB 6 Applications to Visual Basic .NET Keith PleasArchitect, Guided Designkeithp@guideddesign.com

  2. Overview • Language changes – code, object, data • Classic Visual Basic “Ruby” forms to Windows Forms • Upgrade Tools & Utilities • The Upgrade Process - Examples

  3. Structured Exception Handling • Try…Catch…Finally • Throw an error – similar to Err.Raise • "On Error Goto…" still supported • Err object still available (Compatibility) • Depends on “exception classes” than hold information about exception • Block scoping of variables

  4. Structured Exception Handling Example Try ' Some typical processing logic rsRecordset.Update Catch excError as Exception When Expression ' This code runs when an error occurs ' in the code above LogError ("Unable to update the recordset") Finally ' This code always runs, either after the ' Try code if there was no error, or after ' the Catch code if there was rsRecordset.MoveNext End Try

  5. Changes in Data Types • Integer  Short (System.Int16) • Long  Integer (System.Int32) • Long  64-bit value (System.Int64) • Currency  Decimal (System.Decimal) • Variant  Object (System.Object) • New Char holds a single character • Often used for character arrays • Actually 2 bytes to hold Unicode • Fixed-length string not a base .NET type

  6. Declaration Syntax Changes • Multiple declarations on a single line assumed to be the same type • Initial value now supported Dim x, y As Integer ' both are integers Dim intHours As Integer = 20 Dim intMinutes as Integer = intHours * 60

  7. Array Declaration Changes • Must use Dim for initial declaration of arrays – Redim for size change only. • Initial values for arrays supported • Option base always zero • Array size works the same as in VB6, so declaring x(5) gives 6 elements (0…5) Dim strNames() as String = ("Moe", "Larry", "Curly")

  8. Retired Keywords • These keywords are retired and no longer supported • GOSUB (Return has different usage) • DefType statements (such as DefInt, DefStr, etc.) • On x GoTo … (computed GoTo’s) • Let • Option Base 0 | 1 • VarPtr, ObjPtr, StrPtr

  9. Structures Replace UDTs • UDT in VB6 could look like this: Type TransferRecord RecID As Integer Location As String*20 Status As Boolean End Type Closest match in VB.NET is: Structure TransferRecord Public RecID As Integer Public Location As String 'Variable length string! Public Status As Boolean End Structure No fixed-length strings (caveat) <VBFixedString(15)> Public Title As String 'Fixed!

  10. More Replaced Keywords • VarType • GetType in System.Object • Date & Time • System.DateTime • Graphics methods (Line, Circle, …) • System.Graphics.DrawLine • RSet, LSet • PadRight, PadLeft in System.String • Rnd, Randomize • System.Random

  11. Miscellaneous Changes • Option Strict to control implicit casting • No implicit loading of forms (a form is just another class) • Required parentheses on methods, functions, and subroutines • Parameters are ByVal by Default • Block scoping of variables

  12. Class Changes - Properties • Syntax different from VB 6.0 • ReadOnly, WriteOnly • Default (must have parameters) Dim myData as Integer = 10 Public Property Data( ) As Integer Get Return MyData End Get Set(ByVal Value As Integer) myData = Value End Set End Property

  13. Class Changes - Methods • Same general syntax as VB 6.0 • Arguments are now ByVal by default Public Sub DoStuff(x As Integer) … End Sub Public Function GetStuff( ) As Integer … End Function

  14. Class Changes - Events • VB 6.0 Event, RaiseEvent, and WithEvents still supported: Class TimerState Public Event UpdateTime(dblJump As Double) Public Sub TimerTask(Duration As Double) RaiseEvent UpdateTime(Timer - dblJump) End Sub End Class Public WithEvents mText As TimerState Call mText.TimerTask(9.84)

  15. Class Changes - Handling Events • Declared using "Handlesobject.event " Private Sub Button1_Click(ByVal sender as Object, _ ByVal e as EventArgs) Handles Button1.Click … End Sub Dynamic "AddHandler", "RemoveHandler" Private Sub myClick(ByVal sender As Object, _ ByVal e As EventArgs) … End Sub AddHandler Button1.Click, New EventHandler(AddressOf myClick) RemoveHandler Button1.Click, AddressOf myClick

  16. Events are Really Delegates • “Objects That Call Methods of Other Objects” • Similar to function pointers in other languages (like C++) • Reference type inherited from System.Delegate • Type-safe, Secure, Managed Objects • Can be linked (combined) together: d = CType([Delegate].Combine(d, y.cb), OutputDelegate)

  17. Constructors • Sub New replaces Class_Initialize • New runs when object is instantiated Public Sub New( ) … End Sub Can be Overloaded (no keyword) Public Sub New(ByVal i As Integer) … End Sub Public Sub New(ByVal i As Integer, ByVal s As String) … End Sub

  18. Instantiating Objects • Instantiate and initialize objects at the same time or separately ' Separate (as in VB 6.0) Dim my1 As myClass my1 = New myClass( ) 'Default constructor ' At the same time Dim my2 As myClass = New myClass( ) Dim my3 As New myClass( ) ' Other constructors Dim my4 As New myClass(10) Dim my5 As myClass = New myClass(10)

  19. Destructors • Used to clean up resources • Executed when destroyed by Garbage Collection (GC) • Important: destruction may not happen immediately • Replaces Class_Terminate event • Dispose and Finalize Methods • …\FrameworkSDK\Samples\Technologies\ GarbageCollection\VB

  20. Upgrading Data • Easy: ADO to ADO via Interop • Medium: DAO / RDO to ADO (Interop) • DAO / RDO Databinding not supported • Redesign: DAO/RDO/ADI to ADO.NET

  21. VB 6.0 “Ruby” Forms toWindows Forms

  22. Windows Forms Differences • A rich new object library • Coordinate system • Arrange & Dock • New features - Visual Inheritance • API calls to GDI+ Managed Classes

  23. Imports System Imports System.Windows.Forms Public Class Form1 Inherits Form Public Sub New() MyBase.New() InitializeComponent() End Sub Private Sub InitializeComponent() Me.Name = "Form1" Me.Text = "Hello World" End Sub End Class “Hello World” Form Class

  24. Form Code Changes • Form layout • Object changes Me.Move (Screen.Width - Me.Width), _ (Screen.Height - Me.Height) / 2 Me.StartPosition = FormStartPosition.CenterScreen Me.MousePointer = vbHourglass Me.Cursor.Current = Cursors.WaitCursor

  25. Dynamic Layout TextBox1.Anchor = _ AnchorStyles.Top _ Or AnchorStyles.Left) _ Or AnchorStyles.Right) Panel1.Dock = DockStyle.Bottom

  26. Changes to modal dialogs • VB6 code • VB.NET code • Built-in DialogResult property to discover user action Dim frmDialogForm As DialogForm Set frmDialogForm = New DialogForm frmDialogForm.Show vbModal Dim frmDialogForm As New DialogForm frmDialogForm.ShowDialog

  27. Owned forms • Always displayed above “owner form” • Do not interfere with owner form operation • Used for tutorial windows, search and replace box • Can be set by owner form – AddOwnedForm method • Can be set by owned form – Owner property • Also TopMost property

  28. Upgrading APIs (1 of 2) • Generally works with simple APIs • These Need Modification: • AsAny • User Defined Types • AddressOf • Examples: • GetWindowsDirectory • GetOpenFileName • SendMessage

  29. Upgrading APIs (2 of 2) • Consider replacing with GDI+ • Not Supported • ScaleMode (only pixels supported) • AutoRedraw (use Paint event) • HasDC, HDC • DrawMode, DrawStyle, DrawWidth (use Pen object)

  30. Requires Re-architecting

  31. Tools & Utilities for Upgrading

  32. “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 FixIT90210ae-R1672-R1B8ZE Dim vTmp As Variant 'FIXIT: Declare 'vNew' with an early-bound data type FixIT90210ae-R1672-R1B8ZE Dim vNew As Variant Dim frmProp As New frmProperty

  33. Upgrade Wizard • EXE & DLL • Copies project • Creates reports • Links to Help • Four Levels • Issue No Automatic Upgrade • ToDo Requires Finishing • Warning Possible Behavior Change • Note Informational

  34. 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

  35. ’Upgrade Visual Basic 6 Code’ 'UPGRADE_WARNING: Couldn't resolve default property of object Me.Left. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"' 'UPGRADE_WARNING: Couldn't resolve default property of object Me.Width. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"' Me.Left = (VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) - Me.Width) / 2 'UPGRADE_WARNING: Couldn't resolve default property of object Me.Top. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"' 'UPGRADE_WARNING: Couldn't resolve default property of object Me.Height. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"' Me.Top = (VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) - Me.Height) / 2

  36. Upgrading Projects To Visual Basic .NET

  37. Upgrade #1- Form Controls • Data  System.Windows.Forms.Label • Image  System.Windows.Forms.PictureBox • Timer  System.Windows.Forms.Timer • Line  System.Windows.Forms.Label • DirListBox  Compatibility.VB6.DirListBox • FileListBox  Compatibility.VB6.FileListBox

  38. Upgrading ActiveX Controls • Microsoft Windows Common Controls 6.0 • MSCOMCTL.OCX • Interop.ComctlLib.dll • AxInterop.MSComctlLib.dll

  39. VisualBasic.dll • Collection • Constants (vbCancel, vbCrLf… • ControlChars (CrLf, Tab,… • DateAndTime (DateAdd… • ErrObject • FileSystem • Information (IsDate, IsDBNull, … • VBMath

  40. 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

  41. Dealing with Transactions .NET • StoredProcedures • EnterpriseServices(COM+) • .NET Framework Transactions(Manual) VB 6.0 • Stored Procedures • MTS / COM+ Transactions

  42. Dealing with Transactions MTS or COM+ objects were not upgraded The project that you are attempting to upgrade has a reference to the COM+ Services Type Library (Comsvcs.dll) that cannot be upgraded. In Visual Basic 6.0, this library was used to access Microsoft Transaction Services (MTS). In Visual Basic .NET, MTS is replaced by transaction processing functionality in the .NET Framework. Because of differences in the transaction processing models, existing code will need to be deleted and replaced by code that uses .NET transaction processing.

  43. Upgrade #2 – VisData… • MDI Application, test for data access • 35 Forms – 8,343 lines • 1 VB Module – 2,325 lines • 1 Class Module – 103 lines • 3 ActiveX Controls • Common Dialog Control • Data Bound Grid Control • Common Controls • 485KB source (no .FRX or resources)

  44. Upgrade #2: …Results • 22 Minutes, 519 Tasks • 103 Errors • App, Printer, Tag, Data Control • 16 ToDos • Behavior must be checked • 400 Upgrade Warnings • MousePointer, Resize, SelectedIndexChanged • Default properties

  45. Upgrade #3: Duwamish     Dim / ReDim Preserve  MTS objects not upgraded

  46. Upgrade #4: Medisys • 2 Tier Client Server • SQL Server 7.0 access via RDO • MDI Application • Extensive use of ActiveX controls • 129 Forms • 12 VB code modules • 76 Class modules • 134,000 lines of code • Code 95% upgraded (6,700 lines to fix)

  47. Summary • Use Wizard for language & object changes • Upgrading adds value • Stronger type checking • Inheritance • Easier integration with other languages • Better development experience • No limits • Merely upgrading versus using the “.NET Force”

  48. Ask The ExpertsGet Your Questions Answered • 13:00 to 15:00 Thursday 3 July

  49. Community Resources • Community Resources http://www.microsoft.com/communities/default.mspx • Most Valuable Professional (MVP) http://www.mvp.support.microsoft.com/ • Newsgroups Converse online with Microsoft Newsgroups, including Worldwide http://www.microsoft.com/communities/newsgroups/default.mspx • User Groups Meet and learn with your peers http://www.microsoft.com/communities/usergroups/default.mspx

  50. Suggested Reading & Resources The tools you need to put technology to work! TITLE Available Upgrading Microsoft® Visual Basic® 6.0 to Microsoft Visual Basic .NET: 0-7356-1587-X Today • Microsoft Press books are 20% off at the TechEd Bookstore • Also buy any TWO Microsoft Press booksand get a FREE T-Shirt

More Related