Upgrading VB 6.0 Applications to VB.NET - SQL.ru

bracechumpInternet and Web Development

Feb 5, 2013 (4 years and 5 months ago)

312 views

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

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

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

Changes in Data Types

Integer



卨S牴
卹獴敭⹉湴n㘩

䱯湧L



䥮I敧敲
卹獴敭e䥮I㌲)

Long




-
扩琠癡t略 ⡓祳瑥(⹉.琶㐩

Currency



䑥捩浡氠⡓祳瑥洮䑥捩浡氩

噡物V湴n



佢橥捴c⡓祳瑥t⹏扪.捴c

乥眠䍨慲C桯汤猠l⁳楮 汥l捨慲慣瑥r

Often used for character arrays

Actually 2 bytes to hold Unicode

Fixed
-
length string not a base .NET type

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

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

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

UDT in VB6 could look like this:

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

Closest match in VB.NET is:

No fixed
-
length strings (caveat)


<VBFixedString(15)> Public Title As String
'Fixed!

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

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

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

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

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

Declared using "
Handles

object.event
"

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

Dynamic "
AddHandler
", "
RemoveHandler
"

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)

Constructors

Sub New

replaces
Class_Initialize

New

runs when object is instantiated

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

Can be Overloaded (no keyword)

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)

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

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

ADODB


ADODB.dll

Strong, in GAC & PIA

DAO

Interop.DAO.dll

Not Strong

RDO

Interop.RDO.dll

Not Strong

VB 6.0 “Ruby” Forms to

Windows Forms

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


MyBase.New()


InitializeComponent()


End Sub



Private Sub InitializeComponent()


Me.Name = "Form1"


Me.Text = "Hello World"


End Sub


End Class

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

TextBox1.Anchor = _


AnchorStyles.Top _


Or AnchorStyles.Left) _


Or AnchorStyles.Right)

Panel1.Dock = DockStyle.Bottom

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

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)

Generally works with simple APIs

These Need Modification:

AsAny

User Defined Types

AddressOf

Examples:

GetWindowsDirectory

GetOpenFileName

SendMessage

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

ActiveX Documents

Leave in VB 6.0 or
switch to ActiveX EXE
then upgrade

DHTML pages

Navigate to VB.NET

Add
-
In extensibility

Use new object model

Property pages

Property browser

OLE control

WebBrowser control

Tools & Utilities for
Upgrading

“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

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


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_WARNING: Couldn't resolve default property of object Me.Left. Click for more: 'ms
-
help://MS.VSCC.2003/commoner/redir/re
direct.htm?keyword="vbup1037"'

'UPGRADE_WARNING: Couldn't resolve default property of object Me.Width. Click for more: 'ms
-
help://MS.VSCC.2003/commoner/redir/r
edirect.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/red
irect.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

Upgrading Projects To
Visual Basic .NET


Data



System.Windows.Forms.Label


Image



System.Windows.Forms.PictureBox


Timer



System.Windows.Forms.Timer


Line



System.Windows.Forms.Label

Upgrade #1
-

Form Controls


DirListBox


Compatibility.VB6.DirListBox


FileListBox


Compatibility.VB6.FileListBox


Microsoft Windows Common Controls 6.0


MSCOMCTL.OCX



Interop.ComctlLib.dll


AxInterop.MSComctlLib.dll

Upgrading ActiveX Controls

VisualBasic.dll


Collection


Constants (
vbCancel, vbCrLf…


ControlChars (
CrLf, Tab,…


DateAndTime (
DateAdd…


ErrObject


FileSystem


Information (
IsDate, IsDBNull, …


VBMath

VisualBasic.Compatibility.dll

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.

Dealing with Transactions



VB 6.0

Stored
Procedures

MTS / COM+
Transactions



.NET

Stored

Procedures

EnterpriseServices

(COM+)

.NET Framework
Transactions

(Manual)

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…

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

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

bas

cls

KB

IO

Lines

Err

Warn

Min

DSO

0

3

41

1

1319

3

120

1

Bus Logic

2

7

327

1

11036

1

29

5

Data Access

0

1

6

0

189

1

0

1

Workflow

1

1

35

2

1078

0

17

2

3

12

409

4

13622

5

166

9



Dim / ReDim Preserve



MTS objects not upgraded







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)

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”

Ask The Experts

Get Your Questions Answered

13:00 to 15:00 Thursday 3 July

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

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 books

and get a FREE T
-
Shirt


evaluations

© 2003 Microsoft Corporation. All rights reserved.

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