Download presentation
Presentation is loading. Please wait.
Published byTracy Richards Modified over 9 years ago
1
DEV230 Upgrading VB 6 Applications to Visual Basic.NET Keith Pleas Architect, Guided Design keithp@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 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 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 Type TransferRecord RecID As Integer Location As String*20 Status As Boolean End Type Structure TransferRecord Public RecID As Integer Public Location As String 'Variable length string! Public Status As Boolean End Structure Structure TransferRecord Public RecID As Integer Public Location As String 'Variable length string! Public Status As Boolean End Structure Closest match in VB.NET is: No fixed-length strings (caveat) 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 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 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 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) Public WithEvents mText As TimerState Call mText.TimerTask(9.84)
15
Class Changes - Handling Events Declared using "Handles object.event " Private Sub Button1_Click(ByVal sender as Object, _ ByVal e as EventArgs) Handles Button1.Click … End Sub Private Sub Button1_Click(ByVal sender as Object, _ ByVal e as EventArgs) Handles Button1.Click … End Sub 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 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 Dynamic "AddHandler", "RemoveHandler"
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 Public Sub New( ) … End Sub Public Sub New(ByVal i As Integer) … End Sub Public Sub New(ByVal i As Integer, ByVal s As String) … End Sub Public Sub New(ByVal i As Integer) … End Sub Public Sub New(ByVal i As Integer, ByVal s As String) … End Sub Can be Overloaded (no keyword)
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) ' 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 ADODBADODB.dllStrong, in GAC & PIA DAOInterop.DAO.dllNot Strong RDOInterop.RDO.dllNot Strong
21
VB 6.0 “Ruby” Forms to Windows 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
“Hello World” Form Class 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 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
24
Form Code Changes Form layout Object changes Me.Move (Screen.Width - Me.Width), _ (Screen.Height - Me.Height) / 2 Me.StartPosition = FormStartPosition.CenterScreen Me.Move (Screen.Width - Me.Width), _ (Screen.Height - Me.Height) / 2 Me.StartPosition = FormStartPosition.CenterScreen Me.MousePointer = vbHourglass Me.Cursor.Current = Cursors.WaitCursor Me.MousePointer = vbHourglass Me.Cursor.Current = Cursors.WaitCursor
25
Dynamic Layout TextBox1.Anchor = _ AnchorStyles.Top _ Or AnchorStyles.Left) _ Or AnchorStyles.Right) 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 DialogForm Set frmDialogForm = New DialogForm frmDialogForm.Show vbModal Dim frmDialogForm As New DialogForm frmDialogForm.ShowDialog 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 ActiveX DocumentsLeave in VB 6.0 or switch to ActiveX EXE then upgrade DHTML pagesNavigate to VB.NET Add-In extensibilityUse new object model Property pagesProperty browser OLE controlWebBrowser control
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 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 IssueNo Automatic Upgrade ToDoRequires Finishing WarningPossible Behavior Change NoteInformational
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 '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
Data System.Windows.Forms.Label Data System.Windows.Forms.Label Image System.Windows.Forms.PictureBox Image System.Windows.Forms.PictureBox Timer System.Windows.Forms.Timer Timer System.Windows.Forms.Timer Line System.Windows.Forms.Label Line System.Windows.Forms.Label Upgrade #1- Form Controls DirListBox Compatibility.VB6.DirListBox DirListBox Compatibility.VB6.DirListBox FileListBox Compatibility.VB6.FileListBox FileListBox Compatibility.VB6.FileListBox
38
Microsoft Windows Common Controls 6.0 Microsoft Windows Common Controls 6.0 MSCOMCTL.OCX MSCOMCTL.OCX Interop.ComctlLib.dll Interop.ComctlLib.dll AxInterop.MSComctlLib.dll AxInterop.MSComctlLib.dll Upgrading ActiveX Controls
39
VisualBasic.dll Collection Collection Constants ( vbCancel, vbCrLf… Constants ( vbCancel, vbCrLf… ControlChars ( CrLf, Tab,… ControlChars ( CrLf, Tab,… DateAndTime ( DateAdd… DateAndTime ( DateAdd… ErrObject ErrObject FileSystem FileSystem Information ( IsDate, IsDBNull, … Information ( IsDate, IsDBNull, … VBMath VBMath
40
VisualBasic.Compatibility.dll Class DirListBoxEx Inherits Microsoft.VisualBasic.Compatibility.VB6.DirListBox End Class Class DirListBoxEx Inherits Microsoft.VisualBasic.Compatibility.VB6.DirListBox End Class 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.
41
Dealing with Transactions VB 6.0 Stored Procedures MTS / COM+ Transactions.NET Stored Procedures EnterpriseServices (COM+).NET Framework Transactions (Manual)
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 basclsKBIOLinesErrWarnMin DSO03411131931201 Bus Logic273271110361295 Data Access0160189101 Workflow1135210780172 31240941362251669 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 Experts Get Your Questions Answered 13:00 to 15:00 Thursday 3 July
49
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! TITLEAvailable 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 books and get a FREE T-Shirt
51
evaluations evaluations
52
© 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.