introduction:
With the advent of the Microsoft .NET platform and the large number of Windows and Web applications developed in Visual Basic, the issue of migration to VB .Net is timely. So what are the motivations for migrating to VB .NET (or C #)? Should we migrate or not your projects to VB .NET? Visual Basic is it the end of life?
1-Should we upgrade to VB .NET:
The migration to VB .Net is not an obligation, and an application that works perfectly today and meets your needs no reasons to migrate to VB .Net. Nevertheless, it may be an update or new version of your project requires you to choose between Visual Basic and Visual Basic .Net. This choice can not be done by considering several factors and assessing their advantages and disadvantages. Once the decision, we must define the strategy to achieve the migration.
1.1-benefits:
The major new VB .Net:
• Visual Basic .NET is a true object-oriented language (inheritance, polymorphism is a reality).
• Error handling is improved through structured exception handling.
• The management of multi applications is now possible.
The .NET platform:
• Improved scalability: The ADO .Net improve the scalability of your application by promoting offline and reducing the number of connections and DBMS locks. ASP Session management has been improved and can be shared among several servers in a Web farm.
• Increased performance: The Data Reader object was designed for speed of data access. ASP .Net pages are now compiled.
• Easier deployment: The deployment of your application will be made by a simple XCOPY.
2. What strategy to achieve the migration?
Three migration options are available, either completely rewrite your application in Visual Basic .NET, or migrate it totally or partially to migrate.
2.1. Rewriting your application
This option is useful if you want:
• Benefit from improved performance and increased scalability of your application (ASP .Net ADO .Net to use and helps improve)
• Migrate your application but the existing Visual Basic code does not have an equivalent in Visual Basic .NET or it is impossible to migrate.
This solution has the following disadvantages:
• No existing code reusability
• important human and financial investment
2.2. The total migration:
Advantage:
• Reuse of part of the existing code.
• Improved performance and scalability for the parties migrated.
Disadvantages:
• The migrated code may not be effective and will require adjustments.
• Some portions migrated (eg using ADO) does not benefit from the advantages of .NET
The Migrating a Visual Basic project to VB .Net
The project we're studying is an n-tier project respecting the Windows DNA architecture. It consists of an introduction to ASP layer, a layer of business objects that implement business rules and a data persistence layer using ADO. We will migrate a part of the latter layer.
1-General Considerations
1.1. Unsupported features
Some features are not supported by VB .NET and can not be migrated by the wizard:
• The OLE Container control.
• Dynamic Data Exchange (DDE).
• The DAO and RDO data Binding.
• Visual Basic controls 5.
• DHTML applications.
• The Active X Documents.
• The property pages.
Applications using these features should be left unchanged in Visual Basic 6.0 (Property pages for example) or migrating to .NET (processing documents ActiveX controls users).
1.2. Unsupported keywords
Some keywords have been removed from language.
Keyword Description
Def <type> must explicitly declare variables.
Goto / Gosub These instructions can be replaced with If or Select Case.
Gosub / Return These instructions can be replaced by the use of functions or procedures.
Option Base 0 | 1 The tables are indexed at 0.
VarPtr, ObjPtr, StrPtr These functions used to retrieve the memory address some variables are no longer used.
LSet This function allows the assignment data type is no longer
2. The upgrade strategy
It is recommended that you install your project on the machine which will be launched migration. This guarantees that the VB6 project works and that all the elements used by the project (components, controls ...) are available at the time of migration.
Moreover the migration wizard only accepts projects Visual Basic 6. So if you have earlier versions, it is recommended to migrate prior to Visual Basic 6.
The upgrade can be performed in stages, starting with the client part and the business objects, and finally the data objects. It is possible to go beyond the simple port and integrate .NET Framework improvements. Note that the migration will be easier if your application has been designed in a modular way and respects some good programming practices. We will see this in detail later.
3. The migration through practice using the migration wizard VS.Net
The migration of a project can be broken down into several steps we describe below:
1-Choosing what to migrate
It should define which application parts will be migrated by referring to the strategy presented above.
2-Preparing to Migrate Visual Basic 6 project
Corrections can be made to the Visual Basic 6 code before the passage of the upgrade wizard. It is easier to process corrections directly in Visual Basic and let the wizard perform the update instead of the do after the upgrade is complete.
3-Migration of the application via the Migration Assistant from the VS.Net
4- Correction anomalies detected by the wizard
The Assistant corrects only part of the problem; charging the developer to complete the upgrade.
5. Test the application
Once completed making level, we must achieve the regression testing to ensure that the application still has the expected behaviors.
Preparing to Migrate Visual Basic 6 project
Here are some pitfalls to avoid and correct in your Visual Basic 6 project before the completion of migration:
• Use of early bonding and explicit data conversions.
• Use the date data type to store dates.
• Use predefined constants Visual Basic and not the underlying values.
• Prevent the spread of No.
• Using indexed arrays with zeros.
• Avoid fixed-size tables and chains in the definition of typical users.
• Specify the scope of a parameter explicitly. In Visual Basic parameters are passed by reference by default, whereas in .NET they are passed by value.
This step presents a number of recommendations but is not mandatory. To help you in checking your Visual Basic Code Microsoft offers a free download tool "CodeAdvisor".
This tool is available at the following address:
The migration of the application via the Migration Assistant
To migrate our Visual Basic project we will use the migration wizard provided by Microsoft. For that we open our .vbp project with Visual Studio .NET IDE. The wizard proposes therefore to upgrade our project.
We then choisissions the type of project, in this case it is an ActiveX DLL Server type.
We select the location where the new project will be stored.
Finally we launch the migration of our project.
Once the upgrade is complete, an updated report is before us.
This report presents the errors and warnings detected by the tool updates grouped by file. For each error, a detailed description indicates why the code is a problem and how to correct it.
This report is complemented by four types of comments directly integrated into the application code:
• UPGRADE_ISSUE: Refers to lines that keep the code to compile.
• UPGRADE_TODO: Refers to lines that compile but can, at runtime, generate errors.
• UPGRADE_WARNING: Refers to lines that compile and can be cause errors at runtime.
• UPGRADE_NOTE: Refers to the code that compiles for which the transition to VB .Net can cause side effects.
Let us now study the code generated by the upgrade wizard VB .NET and look at the changes.
• Generating a VB .NET class that contains the methods of our previous Visual Basic class.
• Changing data types. The Integer type was replaced by Short. The Variant by Object.
• Changing the call to functions that must now include parentheses.
• Changing certain constants used. E.g. adCmdStoredProc option has been replaced by ADODB.CommandTypeEnum.adCmdStoredProc.
Correction of anomalies detected by the wizard
Despite the changes made by the wizard, several major anomalies were detected but not corrected. We will study two and make the appropriate corrections.
The construction management chain
I got used to serve me as COM + construction chain to define a path to a UDL file that determines the connection string to the database. Upon activation of a chain this component is recovered and used in all DBMS access.
example:
Implements
COMSVCSLib.IObjectConstruct
Private
sConstructString As
String
Private
Sub IObjectConstruct_Construct(ByVal
pCtorObj As
Object)
Const
NomFonction =
"IObjectConstruct_Construct"
Dim
sConstruct As
IObjectConstructString
Set
sConstruct =
pCtorObj
sConstructString = sConstruct.ConstructString
End Sub
To migrate this code, you
have my class inherits from the ServicedComponent class of the
System.EnterpriseServices namespace. This class provides access to COM +
services. Finally, it should override the Construct method. The migrated code becomes:
Public
Class
DataAccess
Inherits System
.EnterpriseServices.ServicedComponent
Private
sConstructString
As
String
Private
Const
NomModule
As
String
=
"DataClient"
Protected Overrides
Sub
Construct
(
ByVal
constructString
As
String
)
sConstructString
=
constructString
End
Sub
'Construct
Writing
to the event log
If
an anomaly is loguée an error in the event log. The correction is to seek the
method to write to the event log. The WriteEntry method LogEvent class of the
System.Diagnostics namespace meets this need.
'UPGRADE_ISSUE: La constante vbLogToNT n'a pas été mise à niveau.
'Cliquez ici : 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2070"'
'UPGRADE_ISSUE: App méthode App.StartLogging - Mise à niveau non effectuée
'Cliquez ici : 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2069"'
App.StartLogging("", vbLogToNT)
'UPGRADE_ISSUE: La constante vbLogEventTypeError n'a pas été mise à niveau.
'Cliquez ici : 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2070"'
'UPGRADE_ISSUE: App méthode App.LogEvent - Mise à niveau non effectuée
'Cliquez ici : 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2069"'
App.LogEvent(Err.Source & " " & Err.Description, vbLogEventTypeError)
The manually migrated code becomes:
Imports
System
.Diagnostics.EventLog
'...
Dim
myLog
As
New
EventLog
(
"Application"
, System
.Environment.MachineName
,
"Classe DataAccess"
)
myLog
.WriteEntry
(
Err.Source
&
" "
&
Err.Description
, EventLogEntryType
.Error
)
Transaction management
Our class is transactional and we must preserve this property.
To do this is to use the mechanisms of declarative transactions proposed by
the System.EnterprisesServices ( ServiceComponents) class namespace.
On the one hand we will define in class header attribute "TransactionAttribute" to define
the transactional properties of the class.
Furthermore we will use the attribute "AutoComplete" about each method to automatically
in
the SetComplete method on success of the transaction is SetAbort on
error.Here's the code before and after migration:
Function
CreerClient
(…)
Dim
ctx
As
ObjectContext
'...
Set
ctx
=
GetObjectContext
If
Not
ctx
Is
Nothing
Then
ctx
.SetComplete
Set
ctx
=
Nothing
End
If
Imports
System
.EnterpriseServices
<
Transaction
(TransactionOption
.Required
)
>
_
Public
Class
DataAccess
Inherits System
.EnterpriseServices.ServicedComponent
<
AutoComplete
()
>
Function
CreerClient
(
ByVal
sSiret
As
String
,
ByVal
sNomClient
As
String
, _
ByVal
iNumero
As
Short, _
ByVal
sRue
As
String
, _
ByVal
sCodePostal
As
String
, _
ByVal
sCommune
As
String
, _
ByRef
vIdentifiant
As
Object)
As
Short
Migration
is now complete (see Comparison migrated codes)
,
it remains only perform regression
tests.
We could continue incorporating new .NET functionality
(replacing COM
components in .NET components, ADO, ADO .Net passage, change in the error
handling ...).
final result :
Vb6 Code:
Option Explicit
Private
Const
NomModule
=
"DataClient"
Function
CreerClient
(
ByVal
sSiret
As
String
, _
ByVal
sNomClient
As
String
, _
ByVal
iNumero
As
Integer, _
ByVal
sRue
As
String
, _
ByVal
sCodePostal
As
String
, _
ByVal
sCommune
As
String
, _
ByRef
vIdentifiant
As
Variant)
As
Integer
On
Error
GoTo
GestionErreur
Dim
oConnexion
As
ADODB
.Connection
Dim
oRecordset
As
ADODB
.Recordset
Dim oCommand As ADODB.Command
Dim ctx As ObjectContext
Dim iCodeRetour As Integer 'contient le code retour de la proc stock
Set
oCommand
=
New
ADODB
.Command
With
oCommand
.Parameters.Append
.CreateParameter
(
"@Retour"
,
adInteger
, _
adParamReturnValue
)
'...
End
With
Set
oConnexion
=
New
ADODB
.Connection
oConnexion
.Open
sConstructString
oConnexion
.BeginTrans
With
oCommand
.CommandType
=
adCmdStoredProc
.CommandText
=
"Insert_Client"
Set
.ActiveConnection
=
oConnexion
End
With
oCommand.Execute , , adExecuteNoRecords
'recuperation du code retour de la procédure stockée
vIdentifiant
=
oCommand
.Parameters
(
"@Retour"
)
.Value
Set
ctx
=
GetObjectContext
If
Not
ctx
Is
Nothing
Then
ctx
.SetComplete
Set
ctx
=
Nothing
End
If
Code vb.net:
Option
Strict Off
Option
Explicit
On
Imports
System
.Diagnostics.EventLog
Imports
System
.EnterpriseServices
<
Transaction
(TransactionOption
.Required
)
>
_
Public
Class
DataAccess
Inherits System
.EnterpriseServices.ServicedComponent
Private
Const
NomModule
As
String
=
"DataClient"
<
AutoComplete
()
>
_
Function
CreerClient
(
ByVal
sSiret
As
String
,
ByVal
sNomClient
As
String
, _
ByVal
iNumero
As
Short,
ByVal
sRue
As
String
,
ByVal
sCodePostal
As
String
, _
ByVal
sCommune
As
String
,
ByRef
vIdentifiant
As
Object)
As
Short
On
Error
GoTo
GestionErreur
Dim
oConnexion
As
ADODB
.Connection
Dim
oRecordset
As
ADODB
.Recordset
Dim
oCommand
As
ADODB
.Command
Dim
iCodeRetour
As
Short
oCommand
=
New
ADODB
.Command
With
oCommand
.Parameters.Append
(
.CreateParameter
(
"@Retour"
, _
ADODB
.DataTypeEnum.adInteger
, _
ADODB
.ParameterDirectionEnum.adParamReturnValue
))
'...
End
With
oConnexion
=
New
ADODB
.Connection
oConnexion
.Open
(sConstructString)
With
oCommand
.CommandType
=
ADODB
.CommandTypeEnum.adCmdStoredProc
.CommandText
=
"Insert_Client"
.ActiveConnection
=
oConnexion
End
With
oCommand
.Execute
(, , ADODB
.ExecuteOptionEnum.adExecuteNoRecords
)
vIdentifiant
=
oCommand
.Parameters
(
"@Retour"
)
.Value
If
(
Not
oRecordset
Is
Nothing
)
Then
If
oRecordset
.State
=
ADODB
.ObjectStateEnum.adStateOpen
Then
_
oRecordset
.Close
()
End
If
If
(
Not
oConnexion
Is
Nothing
)
Then
If
oConnexion
.State
=
ADODB
.ObjectStateEnum.adStateOpen
Then
_
oConnexion
.Close
()
End
If
End
If
Post a Comment