Dev230 upgrading vb 6 applications to visual basic net
Sponsored Links
This presentation is the property of its rightful owner.
1 / 52

DEV230 Upgrading VB 6 Applications to Visual Basic .NET PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Dev230 upgrading vb 6 applications to visual basic net

DEV230Upgrading VB 6 Applications to Visual Basic .NET

Keith PleasArchitect, Guided



  • Language changes – code, object, data

  • Classic Visual Basic “Ruby” forms to Windows Forms

  • Upgrade Tools & Utilities

  • The Upgrade Process - Examples

Structured exception handling

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

Structured exception handling example

Structured Exception Handling Example


' Some typical processing logic


Catch excError as Exception When Expression

' This code runs when an error occurs

' in the code above

LogError ("Unable to update the recordset")


' This code always runs, either after the

' Try code if there was no error, or after

' the Catch code if there was


End Try

Changes in data types

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

Declaration syntax changes

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

Array declaration changes

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")

Retired keywords

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

Structures replace udts

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!

More replaced keywords

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

Miscellaneous changes

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

Class changes properties

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


Return MyData

End Get

Set(ByVal Value As Integer)

myData = Value

End Set

End Property

Class changes methods

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

Class changes events

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)

Class changes handling events

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

Events are really delegates

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)



  • 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

Instantiating objects

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)



  • 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

Upgrading data

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

Dev230 upgrading vb 6 applications to visual basic

VB 6.0 “Ruby” Forms toWindows Forms

Windows forms differences

Windows Forms Differences

  • A rich new object library

  • Coordinate system

  • Arrange & Dock

  • New features - Visual Inheritance

  • API calls to GDI+ Managed Classes

Hello world form class

Imports System

Imports System.Windows.Forms

Public Class Form1

Inherits Form

Public Sub New()



End Sub

Private Sub InitializeComponent()

Me.Name = "Form1"

Me.Text = "Hello World"

End Sub

End Class

“Hello World” Form Class

Form code changes

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

Dynamic layout

Dynamic Layout

TextBox1.Anchor = _ AnchorStyles.Top _ Or AnchorStyles.Left) _

Or AnchorStyles.Right)

Panel1.Dock = DockStyle.Bottom

Changes to modal dialogs

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


Owned forms

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

Upgrading apis 1 of 2

Upgrading APIs (1 of 2)

  • Generally works with simple APIs

  • These Need Modification:

    • AsAny

    • User Defined Types

    • AddressOf

  • Examples:

    • GetWindowsDirectory

    • GetOpenFileName

    • SendMessage

Upgrading apis 2 of 2

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)

Requires re architecting

Requires Re-architecting

Dev230 upgrading vb 6 applications to visual basic

Tools & Utilities for Upgrading

Code advisor a k a fixit

“Code Advisor” a.k.a “FixIt”


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

Upgrade wizard

Upgrade Wizard

  • EXE & DLL

  • Copies project

  • Creates reports

  • Links to Help

  • Four Levels

    • IssueNo Automatic Upgrade

    • ToDoRequires Finishing

    • WarningPossible Behavior Change

    • NoteInformational

Before upgrade wizard

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 visual basic 6 code

’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

Dev230 upgrading vb 6 applications to visual basic

Upgrading Projects To Visual Basic .NET

Upgrade 1 form controls

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

Upgrading activex controls

Upgrading ActiveX Controls

  • Microsoft Windows Common Controls 6.0


    • Interop.ComctlLib.dll

    • AxInterop.MSComctlLib.dll

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

Dealing with transactions

Dealing with Transactions


  • StoredProcedures

  • EnterpriseServices(COM+)

  • .NET Framework Transactions(Manual)

VB 6.0

  • Stored Procedures

  • MTS / COM+ Transactions

Dealing with transactions1

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.

Upgrade 2 visdata

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)

Upgrade 2 results

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

Upgrade 3 duwamish

Upgrade #3: Duwamish

 Dim / ReDim Preserve

 MTS objects not upgraded

Upgrade 4 medisys

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)



  • 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”

Ask the experts get your questions answered

Ask The ExpertsGet Your Questions Answered

  • 13:00 to 15:00 Thursday 3 July

Community resources

Community Resources

  • Community Resources

  • Most Valuable Professional (MVP)

  • Newsgroups

    Converse online with Microsoft Newsgroups, including Worldwide

  • User Groups

    Meet and learn with your peers

Suggested reading resources

Suggested Reading & Resources

The tools you need to put technology to work!



Upgrading Microsoft® Visual Basic® 6.0 to Microsoft Visual Basic .NET: 0-7356-1587-X


  • Microsoft Press books are 20% off at the TechEd Bookstore

  • Also buy any TWO Microsoft Press booksand get a FREE T-Shirt

Dev230 upgrading vb 6 applications to visual basic


Dev230 upgrading vb 6 applications to visual basic

© 2003 Microsoft Corporation. All rights reserved.

This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.

  • Login