Dev230 - ispon

tunisianbromidrosisInternet and Web Development

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

152 views

DEV230

Upgrading VB 6
Applications To Visual
Basic .NET

2

of 48

Overview


Language changes


code, object, data


Classic Visual Basic “Ruby” forms to
Windows Forms


Upgrade Tools and Utilities


The Upgrade Process
-

Examples

3

of 48

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

of 48

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

of 48

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

of 48

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

of 48

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

of 48

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

of 48

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!

10

of 48

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

of 48

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

of 48

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

of 48

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

of 48

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

of 48

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
"

16

of 48

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

of 48

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)

18

of 48

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

of 48

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

of 48

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

22

of 48

Windows Forms Differences


A rich new object library


Coordinate system


Arrange & Dock


New features
-

Visual Inheritance


API calls to GDI+ Managed Classes

23

of 48

“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

24

of 48

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

of 48

Dynamic Layout

TextBox1.Anchor = _


AnchorStyles.Top _


Or AnchorStyles.Left) _


Or AnchorStyles.Right)

Panel1.Dock = DockStyle.Bottom

26

of 48

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

of 48

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

of 48

Upgrading APIs (1 of 2)


Generally works with simple APIs


These Need Modification:


AsAny


User Defined Types


AddressOf


Examples:


GetWindowsDirectory


GetOpenFileName


SendMessage

29

of 48

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

of 48

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 And Utilities For
Upgrading

32

of 48

“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

of 48

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

of 48

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

of 48

’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

37

of 48


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

38

of 48


Microsoft Windows Common Controls 6.0


MSCOMCTL.OCX


Interop.ComctlLib.dll


AxInterop.MSComctlLib.dll

Upgrading ActiveX Controls

39

of 48

VisualBasic.dll


Collection


Constants (
vbCancel, vbCrLf…


ControlChars (
CrLf, Tab,…


DateAndTime (
DateAdd…


ErrObject


FileSystem


Information (
IsDate, IsDBNull, …


VBMath

40

of 48

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.

41

of 48

Dealing With Transactions



VB 6.0


Stored
Procedures


MTS / COM+
Transactions



.NET


Stored

Procedures


EnterpriseServices

(COM+)


.NET Framework
Transactions

(Manual)

42

of 48

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

of 48

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

of 48

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

of 48

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







46

of 48

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

of 48

Summary


Use Wizard for language and 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

of 48

Suggested Reading And Resources


Microsoft Press books are
20%

off at the TechEd Bookstore
and also at the Ask the Experts area in the Expo Hall


Visit Amazon.com today to receive
40%

off selected titles

The tools you need to put technology to work!

TITLE

Available

Price

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























































Today







































$59.99

49

of 48

© 2003 Microsoft Corporation. All rights reserved.

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