Report

Visual Basic to Visual Basic .NET Migration Approach

To view this page ensure that Adobe Flash Player version 9.0.124 or greater is installed.

Get Adobe Flash player
Please login or register to make a comment!

Visual Basic to Visual Basic .NET Migration Approach By Meghana Deshpande , Manu Gupta , Prashant Halari , Dhananjay Katre , Narayana Rao Surapaneni Date: Jun 27, 2003 Sample Chapter is provided courtesy of Prentice Hall Professional . Return to the article Work through the phases of migrating from Visual Basic 6.0 applications to Visual Basic .NET. You will see what features the upgrade wizard does not upgrade from 6.0, and that will set you on your way to a complete upgrade.

As described in the previous chapter, the Visual Basic language has undergone major changes, and many new features have been incorporated to transform it into a true object-oriented language. Many keywords that led to unstructured programming have been removed from the language. Considering the amount of changes, it is necessary to assess if migrating Visual Basic applications to Visual Basic .NET is feasible.

As discussed in Chapter 1 this activity is carried out during the assessment phase of migration. This chapter will help the developers to make an assessment about migrating a Visual Basic application. It will also give an overview of the steps required for migrating Visual Basic 6.0 applications to Visual Basic .NET.

It also covers the pre-migration changes ... more. less.

so that upgrading Visual Basic 6.0 applications to Visual Basic .NET is smooth. Road Map to Migration Migration of any Visual Basic 6.0 application to Visual Basic .NET requires the following major steps: 1. Perform an assessment based on project selection guidelines 2.<br><br> Perform changes based on pre-migration recommendations (covered in detail later in this chapter) 3. With the help of the upgrade wizard, upgrade the Visual Basic 6.0 application to Visual Basic .NET 4. Perform changes based on post-migration recommendations (covered in detail in Chapter 4) The path for migrating a Visual Basic 6.0 application to Visual Basic .NET is represented in Figure 3-1 .<br><br> Brief descriptions for each of these steps are given in the subsequent subsections. Figure 3-1 . Phases of Migration to Visual Basic .NET.<br><br> Assessment Before starting the actual migration process, it is necessary for a developer or project manager to assess the application under consideration. The objective of this phase is to understand the extent of changes required for migration and come up with an appropriate project plan for the migration. Considering the nature of the application and the amount of changes to be made in the Visual Basic 6.0 application, it can be classified in one of the following three categories: 1.<br><br> Applications with the following features are considered simple and can be migrated easily: o Dynamic-link libraries (DLLs) o Application with only Forms or MDI Forms 2. Applications with the following features can be considered of medium complexity and can be migrated with changes: o Data binding using ActiveX Data Objects (ADO) o Graphics o Menus o Windows API o User controls 3. Applications that require extensive code changes can be regarded as advanced applications: o ActiveX Dynamic HTML (DHTML) applications o Active X documents o Legacy one-tier Microsoft Access database with DAO or Remote Data Objects (RDO) data binding o Dynamic Data Exchange (DDE) o Object Linking and Embedding (OLE) Once the Visual Basic 6.0 application has been classified as simple, medium, or advanced, efforts and schedules can be planned for migrating the application.<br><br> Once the decision to migrate the application is made, the premigration changes are performed in the Visual Basic 6.0 application. Pre-migration Changes Overview The purpose of pre-migration is to make changes in existing Visual Basic 6.0 projects to smooth the migration to Visual Basic .NET. If the original application has been developed using earlier versions of Visual Basic (earlier than version 6.0), the application has to be first upgraded to Visual Basic 6.0.<br><br> Then, certain changes have to be made to the Visual Basic 6.0 application based on the pre-migration recommendations. These recommendations are covered in detail in later sections of this chapter. It is important to note that the pre-migration changes are not mandatory.<br><br> The upgrade wizard will give errors and warnings for all the code that is not supported in Visual Basic .NET in the form of an upgrade report. Based on the upgrade report, the developer can make changes to the upgraded Visual Basic .NET code. However, if the developer makes pre-migration changes, the upgrade wizard will automatically do some of the required changes during the upgrade process.<br><br> Thus the developer would have to concentrate only on those parts of the application that are not upgraded automatically by the upgrade wizard. Upgrade Wizard Overview Once the pre-migration changes have been made to the Visual Basic 6.0 application, the changed application can be opened directly in Visual Studio .NET. This automatically invokes the upgrade wizard provided as part of Visual Studio .NET.<br><br> The upgrade wizard will convert the Visual Basic 6.0 application into the equivalent Visual Basic .NET application. The Visual Basic forms are converted into the equivalent Windows Forms. All the controls on the form are converted into their equivalent Windows controls.<br><br> The upgrade wizard makes all the necessary syntax changes for converting the code into Visual Basic .NET. However, the upgrade wizard does not perform upgrades on Visual Basic 6.0 features such as DDE, OLE, DAO, and RDO data bindings. The upgrade wizard produces an upgrade report.<br><br> This report gives a status of the upgrade process, listing all the features that were upgraded by the wizard and all those features that were not upgraded. The report also gives links to Microsoft Developer Network (MSDN) topics to help developers work on the issues identified by the upgrade wizard. The upgrade wizard is covered in more detail later in this chapter.<br><br> Post-migration Changes Overview As seen in the previous section, once a Visual Basic 6.0 application is opened in Visual Studio .NET, the upgrade wizard is invoked to facilitate the upgrade to Visual Basic .NET. On running the upgrade wizard, an upgrade report is produced that lists all the issues faced and the problem areas identified during the upgrade process. Based on the upgrade report findings and the desired functionality in Visual Basic .NET, the developer needs to change the code in Visual Basic .NET.<br><br> The objective of this phase is to write code in Visual Basic .NET to obtain the same functionality in the migrated code. To simplify the post-migration changes, the changes have been categorized by the commonly used features and functionality in Visual Basic 6.0. A few of the features are as follows: " Help functionality " Database operations " Windows API calls " Drag and drop functionality " Menus " MDI forms The post-migration recommendations are covered in detail in Chapter 4.<br><br> Pre-migration Recommendations Visual Basic .NET is radically different from Visual Basic 6.0. To convert Visual Basic 6.0 applications into Visual Basic .NET, developers need to learn the features of the .NET Framework. This section covers the changes between Visual Basic 6.0 and Visual Basic .NET languages.<br><br> Microsoft has also provided some recommendations on the changes to be made to an existing Visual Basic 6.0 application so that the upgrade to Visual Basic .NET is as smooth as possible. The next section covers the language and syntax changes as well as the pre-migration recommendations given by Microsoft. Understanding Language and Syntax Changes To migrate Visual Basic 6.0 applications to Visual Basic .NET, it is essential to understand the differences between Visual Basic 6.0 and Visual Basic .NET, discussed in the following subsection.<br><br> Variant Visual Basic 6.0 languages support a data type called as variant . The variant is a flexible data type; it can accommodate all other types. If a variable is not declared with any type, it is regarded as a variant type variable.<br><br> Visual Basic bases the type of the variant variable on its contents. For example, if the variant variable contains a string, Visual Basic handles the variable as a string type variable. If it contains an integer, it is handled as an integer type variable.<br><br> Consider that there are multiple variables declared on one line as shown in the following code snippet. Dim Counter1, Counter2, Counter3 As Integer Here only the variable Counter3 is declared to be of type Integer . The other two variables Counter1 and Counter2 are of type variant .<br><br> In Visual Basic .NET, the variant data type is not supported. All the variables of variant data type get converted to object type in the Visual Basic .NET code. Object is a generic class in the Microsoft .NET Framework class library from which all other classes are derived.<br><br> Currency In Visual Basic 6.0, the currency data type is used for performing financial calculations. currency types are not supported in Visual Basic .NET. During the upgrade process, the upgrade wizard will change all currency data types to decimal type.<br><br> However, to smooth the migration process all currency variables in Visual Basic 6.0 applications can be converted to double type before upgrading to Visual Basic .NET, as the following code snippet demonstrates: Dim Sal_Amount As Currency In this case, it is recommended to change the above variable declaration to double type: Dim Sal_Amount As Double If the Visual Basic 6.0 application uses the FormatCurrency function, the application is upgraded and works well in Visual Basic .NET with the help of the Microsoft.VisualBasic.Compatibility class. Date In Visual Basic 6.0 a date variable is stored internally in double format and all the manipulations on double format are applicable to the date type. Hence double type of data has been frequently used for date manipulations.<br><br> In Visual Basic .NET, double type variables cannot be used for date manipulation. Fixed-Length Strings Fixed-length strings are not supported in Visual Basic .NET. In Visual Basic .NET fixed-length strings cause a problem when used in user-defined types such as structures .<br><br> So it is recommended to use strings instead of fixed-length strings. Type User-defined types can be defined in a Visual Basic application as shown in this code snippet: Type Customer Customer_ID As Integer Customer_Name As String End Type In Visual Basic .NET, the Type keyword is not supported. Structure is the keyword for declaring user-defined types in Visual Basic .NET as follows: Structure Customer Customer_ID As Integer Customer_Name As String End Structure Visual Basic .NET also supports access modifiers for the members of the structure.<br><br> Access modifiers like public , private , and friend can be used with the members of a structure. Many of the Windows API function calls require the data types to be passed as parameters. Thus, when these function declarations are upgraded to Visual Basic .NET, they will get converted into equivalent structures.<br><br> In Visual Basic .NET, user-defined types are called structures. Structures are in many ways similar to a class. Structures can have constructors, methods, properties, and so on.<br><br> Structures, however, do not support inheritance. Also, structure variables are value type variables, and they do not use heap memory. A structure inherits from the System.ValueType object.<br><br> Classes, on the other hand, are reference type variables. Instances of classes are normally allocated on heap memory. Classes support inheritance.<br><br> Even though user-defined types such as classes and structures are supported in Visual Basic .NET, fixed-length strings in user-defined types are not supported. In such cases, data marshalling techniques must be used in Visual Basic .NET. Empty Variants are initialized to Empty , which automatically gets converted to zero when used in a numeric expression, or to an empty string when used in a string expression.<br><br> Empty is not supported in Visual Basic .NET. Object type variables are initialized to Nothing in Visual Basic .NET. Null and Null Propagation Null values indicate that a variable contains no valid data.<br><br> If any part of an expression evaluates to null , the entire expression evaluates to a null . Null propagation is not supported in Visual Basic .NET. Normally null values are obtained when database operations are involved.<br><br> Because null propagation is not supported, Visual Basic .NET offers a database-programming model, which test fields explicitly for null before retrieving their values. Variants containing null are marshalled into the CLR as objects of type DBNull . Def<Type> Def<Type> statements are used to set the default data type for variables in Visual Basic 6.0.<br><br> The following code snippet shows an example of the DefInt type: DefInt A-D Private Sub Form_Click() AVariable = 1 BVariable = "Error"'this will give an error MsgBox (AVariable) MsgBox (BVariable) End Sub The DefType statements are used at the module level. In the preceding code snippet, all variables that begin with A, B, C, and D are defined to be of type integer . The sample code gives an execution error when the string "Error" is assigned to the variable BVariable, which is of integer type variable.<br><br> This type of data type definition is not supported in Visual Basic .NET. Scope of Local Variables In Visual Basic 6.0, if a variable is declared in a block, it is also visible outside the scope of the block, and the Visual Basic 6.0 code can use the value inside the variable. So the following code is valid in Visual Basic 6.0: Do Dim iLoopCounter As Long iLoopCounter = iLoopCounter + 1 Loop Until iLoopCounter > 20 MsgBox(iLoopCounter) This type of code is not supported in Visual Basic .NET.<br><br> If a variable needs to be used outside a block in Visual Basic .NET, it needs to be declared outside of the block. If the preceding code is written in Visual Basic .NET, it will give a compilation error that the variable iLoopCounter has not been declared. Object Finalization There is a very huge difference in the way objects are cleared from memory between Visual Basic 6.0 and Visual Basic .NET.<br><br> In Visual Basic 6.0, the COM reference-counting mechanism is used to free up the object instances. When objects are not in cycles, reference counting will immediately detect when an object is no longer being used and will run its termination code. In Visual Basic .NET, a garbage collector does the job of cleaning up the unreferenced objects.<br><br> The garbage collector is a feature of the Microsoft .NET Framework, and it is applicable for all the languages supported by the Microsoft .NET Framework. The developer does not know when the garbage collector will collect the unreferenced objects. This is known as undeterministic garbage collection.<br><br> The garbage collector also compacts the memory that is in use. The .NET Framework provides a class called GC that contains a method Collect . Developers can directly call this method to force garbage collection.<br><br> However, calling the Collect method too often can cause performance issues, because the garbage collector suspends all executing threads before performing garbage collection. Hence it is better to let the garbage collector determine when to perform the garbage collection. Arrays In Visual Basic 6.0, the Option Base statement is used to determine the lower bound if a range is not specified in the declaration.<br><br> We can specify arrays with any lower bounds. This is not supported in Visual Basic .NET. All arrays in Visual Basic .NET have to start off with a base of 0 to make the language interoperable with the other languages supported in the Microsoft .NET Framework.<br><br> In fact, the Option Base statement is not supported in Visual Basic .NET. ReDim In Visual Basic 6.0, ReDim is normally used to redimension an array that has been declared earlier. If the array has not been declared earlier, it is declared with the ReDim statement.<br><br> In Visual Basic .NET, the ReDim statement can be used only if the array has been declared earlier. Error Handling In Visual Basic 6.0, error handling is done with the help of On Error statements. Normally this statement is placed at the beginning of a block of code, and it handles any errors that occur in that block of code.<br><br> This is not a structured way of handling errors. Visual Basic .NET has vastly improved the error-handling techniques. In Visual Basic .NET, conditions in which code fails are known as exceptions.<br><br> A block, which can throw an exception, is enclosed within a try / catch block. If the block throws an exception, it is caught by the code in the catch block. The catch block can handle the exception on its own, or it can rethrow the exception to a higher exception-handling code.<br><br> Function Return Values To return a value from a function, in Visual Basic 6.0, the function name is set to the value that is to be returned. In Visual Basic .NET, the desired value is returned by the Return statement. Assignment In Visual Basic 6.0, there are two forms of assignment: Let assignment (the default) and Set assignment.<br><br> In Visual Basic .NET, there is only one form of assignment. x = y means to assign the value of variable or property y to the variable or property x. The value of an object type variable is the reference to the object instances, so if x and y are reference type variables, a reference assignment is performed.<br><br> Calling Procedures In Visual Basic 6.0, two forms of procedure calls are supported, one using the Call statement, which requires parentheses around the list of arguments, and one without the Call statement, which does not require parentheses around the argument list. Visual Basic .NET makes it mandatory to have parentheses in procedure calls. Static Procedures In Visual Basic 6.0, procedures can be declared with the Static keyword, which indicates that the procedure's local variables are preserved between calls.<br><br> In Visual Basic .NET, the Static keyword is not supported in the procedure, and all static local variables need to be explicitly declared with the Static statement. Parameter Passing In Visual Basic 6.0 the developer can define the parameter as either ByRef (by reference) or ByVal (by value). If the developer does not specify it, the parameter passing defaults to ByRef .<br><br> This has changed to ByVal in Visual Basic .NET: All parameters are ByVal by default. So the developer has to declare a subroutine or function explicitly as ByRef to get it to change a parameter. Optional Parameters In Visual Basic 6.0, optional Variant parameters with no default values are initialized to a special error code that can be detected by using the IsMissing function.<br><br> Visual Basic .NET requires that a default value be specified for all optional parameters. ParamArray Parameters In Visual Basic 6.0, when variables are passed to a ParamArray argument, they can be modified by the called function. However when variables are passed to a ParamArray argument in Visual Basic .NET, they cannot be modified by the called function.<br><br> ByRef ParamArray elements are not supported. As Any In Visual Basic 6.0, a parameter of a native API can be declared As Any , in which case a call to the native API could pass in any data type. This was supported to enable calling APIs whose parameters supported two or more data types.<br><br> In Visual Basic .NET, overloaded Declare statements have to be defined, and it allows a native API to be called with two or more data types. Implements In Visual Basic 6.0, the Implements statement is used to specify an interface or class that will be implemented in the class module. The Implements statement in Visual Basic .NET is different than in Visual Basic 6.0 with respect to the following points: 1.<br><br> In Visual Basic 6.0, the Implements statement is used to indicate that the current class is implementing some other class. In Visual Basic .NET, the Implements keyword can be used only for interfaces. 2.<br><br> Every method that implements an interface method requires an Implements keyword at the end of the method declaration statement. This will specify what interface method it implements. Property Access In Visual Basic 6.0, the Get , Let , and Set property functions for a specific property can be declared with different levels of accessibility.<br><br> For example, the property Get function can be Public , whereas the property Let is Friend . In Visual Basic .NET, the Get and Set functions for a property must both have the same level of accessibility. Default Properties In Visual Basic 6.0, any member can be marked as a default for the class.<br><br> All built-in controls of Visual Basic 6.0 have some default properties. In Visual Basic .NET, only those properties that take parameters can be marked as default. It is common for those properties with parameters to be indexers into a collection.<br><br> Enumerations In Visual Basic 6.0, enumeration constants can be referenced without qualification. In Visual Basic .NET, enumeration constants can be referenced without qualification only if an Import for the enumeration is added at the file or project level. While Statement In Visual Basic 6.0, While statements can end with a Wend statement.<br><br> However, in Visual Basic .NET, to be consistent with other block structures, the terminating statement for While is End While . On..GoTo and On..GoSub In Visual Basic 6.0, statements such as On <Expression> GoTo and On <Expression> GoSub are supported. Because these are unstructured ways of coding, they are not supported in Visual Basic .NET.<br><br> GoSub Return In Visual Basic 6.0, the GoSub Return statement is used to branch to a particular subroutine and to return from a subroutine within a procedure. GoSub Return is not a structured way of coding and is not supported in Visual Basic .NET. LSet Statement In Visual Basic 6.0, the LSet statement is used to left-align a string within a string variable.<br><br> LSet pads a string with spaces to make it a specified length or copies a variable of one user-defined type to a variable of a different user-defined type. This is not supported in Visual Basic .NET. VarPtr, StrPtr, and ObjPtr In Visual Basic 6.0, VarPtr , StrPtr , and ObjPtr return the addresses of variables as integers, which can then be passed to API functions that take addresses, such as RtlCopyMemory .<br><br> VarPtr returns the address of a variable, StrPtr returns the address of a string, and ObjPtr returns the address of an object. These are not supported in Visual Basic .NET. File Handling File input and output is part of the Visual Basic 6.0 language with statements such Open, Close, Input, and Output.<br><br> In Visual Basic .NET, all file input and output operations are removed from the language and the functionality has been shifted into the Microsoft .NET Framework class library. Specifically, the classes in the namespace System.IO are used for file-handling purposes in the Microsoft .NET Framework. Resource Files Visual Basic 6.0 supports one .res file per project.<br><br> Visual Basic .NET has rich support for resources. Forms can be bound to retrieve resources automatically from the new .resX -formatted resource files. Any CLR class can be stored in a .resX file.<br><br> Visual Basic Forms Visual Basic 6.0 has its own forms package for creating graphical Windows applications. Windows Forms is a new forms package for Visual Basic .NET. It resides in the System.windows.Forms namespace of the Microsoft .NET class library.<br><br> Because Windows Forms is built from the ground up to target the CLR, it can take advantage of all of its features. Caption Property In Visual Basic 6.0, the caption property is used to display the text in the title bar of the form. It is also used to display the text on the controls such as Label, Menu bar, and so on.<br><br> The caption property has been changed to the text property in Visual Basic .NET. Fonts In Visual Basic 6.0, Windows Forms can use any Windows fonts. However, Visual Basic .NET can use only true type or open type fonts.<br><br> Control Arrays A control array is a group of controls that share the same name and type. They also share the same event procedures. A control array has at least one element and can grow to as many elements as your system resources and memory permit.<br><br> Elements of the same control array have their own property settings. In Visual Basic .NET, the Windows Form architecture natively handles many of the scenarios for which control arrays were used. For instance, in Windows Forms you can handle more than one event on more than one control with a single event handler.<br><br> WebClasses A WebClass is a Visual Basic component that resides on a Web server and responds to input from the browser. A WebClass typically contains WebItems that it uses to provide content to the browser and expose events. Web Forms is a Microsoft .NET Framework feature that can be used to create a browser-based user interface for Web applications.<br><br> Visual Basic .NET has a what you see is what you get (WYSIWYG) designer for graphical Web Form creation using controls from the Toolbox. This gives Web user-interface development the same feel as Windows development. Also, when the project is built, the Internet Information Services (IIS) server does not have to be stopped and restarted to deploy the changes in the application.<br><br> ActiveX Documents ActiveX documents are Visual Basic 6.0 applications that are packaged so that they can appear within Internet browser windows. They can also be used in other containers such as Microsoft Office Binder. They offer built-in viewport scrolling, hyperlinks, and menu negotiation.<br><br> ActiveX documents are not supported in Visual Basic .NET. Understanding the Data Type Changes Table 3-1 lists all the data type changes that occur in moving from Visual Basic to Visual Basic .NET. These changes have been incorporated into the language to conform to the CTS standards.<br><br> This helps in maintaining interoperability among the various languages supported in Microsoft .NET Framework. All the types mentioned in Visual Basic .NET are classes that represent the base data types, and they are located in the System namespace of the Microsoft .NET Framework class library. Steps for Pre-migration Changes This section gives some coding recommendations for the Visual Basic 6.0 application.<br><br> These pre-migration changes help smooth the upgrade of the Visual Basic 6.0 application to Visual Basic .NET. " Declare all variables on separate lines and ensure that all variables are assigned a data type wherever possible: Dim FirstName, LastName As String Table 3-1. Data Type Changes from Visual Basic 6.0 to Visual Basic .NET Visual Basic 6.0 Data Type Visual Basic 6.0 Data Type Size (Bits) Visual Basic .NET Data Type Visual Basic .NET Data Type Size (Bits) Byte 8 Byte 8 Short 8 Int16 16 Integer 16 Int32 32 Long 32 Int64 64 Single 32 Single 32 Double 64 Double 64 Boolean 16 Boolean Char Char 16 Decimal 96 Decimal 96 Object 32 Object String String For example, when the preceding code is migrated to Visual Basic .NET using the upgrade wizard, the variable LastName is retained as String .<br><br> However, because FirstName is a Variant in the Visual Basic 6.0 code, it is converted into an Object type and a warning will be issued by the upgrade wizard. To avoid that warning, declare the variable and assign the proper data types: Dim FirstName As String Dim LastName As String " Declare all arrays to be zero-based arrays: Dim Name (1 to 10) As String In Visual Basic 6.0, arrays can be defined with lower and upper bounds of any integer number. One can also use ReDim to reassign a variant as an array.<br><br> Arrays in Visual Basic .NET must have a lower bound of zero, and ReDim cannot be used unless the variable was previously declared with Dim As Array . The upgrade wizard will automatically convert the preceding array base to zero. However, the developer can do the same before migration as follows: Dim Name (0 to 10) As String " Check for arrays and fixed-length strings in user-defined types and change them as recommended in the following code snippet.<br><br> When the following code is upgraded to Visual Basic .NET, it fails at runtime because the user-defined types are converted into structures and members of the structure that require parameterless constructors require initialization: Private Type Customer Customer_ID (5) As Integer Customer_Name As String * 50 End Type Sub CustomerData() Dim CustomerA As Customer End Sub This code needs to be corrected as shown in the following example. The type has to be defined without the size, and the fixed-length string has to be defined without the length. When a variable of the type Customer is defined as shown, the code can then ReDim the array size and fix the length of the string to the desired one.<br><br> If the user-defined types are defined as shown, developers will not have to make changes to the upgraded code: Private Type Customer Customer_ID () As Integer Customer_Name As String End Type Sub CustomerData () Dim CustomerA As Customer ReDim CustomerA.Customer_ID(5) As Integer CustomerA.Customer_Name = String$(50, " ") End Sub " Use intrinsic constants rather than relying on their underlying values. Intrinsic constants such as VbMaximized , VbMinimized , VbOK , and VbCancel should be used in place of their actual numerical values. " Boolean comparisons.<br><br> AND and OR operators could be used in Boolean comparisons as shown in the following code snippet: If intVariable1 and intVariable2 Then MsgBox "Condition passed" End If If the variables intVariable1 and intVariable2 are defined as integer variables, a bitwise And operation is performed and wrong results can be obtained. The And operator works properly only on Boolean values, and hence the preceding code needs to be changed as follows: If (intVariable1 <>0) and (intVariable1 <> 0) Then MsgBox("Condition passed") End If There are other operators such as Eqv and Imp that are supported in Visual Basic 6.0. Eqv performs logical equivalence on two expressions.<br><br> The Imp operator performs a logical implication of two expressions. These operators are not supported in Visual Basic .NET. However, any Visual Basic 6.0 code that contains these operators gets upgraded with the help of the Microsoft.VisualBasic.Compatibility namespace.<br><br> " Remove processing actions from If conditions: If len(sCustomerName) = 0 And (isCustomer(CustId)) Then MsgBox "Customer Found" End If In this example a processing condition has been added to the If condition. Even though it will be upgraded and work properly in Visual Basic .NET, it is recommended that the processing actions not be part of the If conditions. So the code can be changed as follows.<br><br> Note that parentheses have been put around the message text in the MsgBox function. Dim bValueFound As Boolean bValueFound = isCustomer(CustId) If len(sCustomerName) = 0 And (bValueFound) Then MsgBox ("Customer Found") End If " Use early binding wherever possible. In the following code sample, there is a label named lblCodeSnippet6 on the form.<br><br> The code snippet creates an object and assigns the label on the form to that object. The caption property of the newly created object is set to some new value. These values get updated on the form.<br><br> Dim objName1 As Object Set objName1 = Me.lblCodeSnippet6 objName1.Caption = "Some New Value" When this code gets migrated to Visual Basic .NET, the upgrade wizard cannot resolve the Caption property of the object because it is late bound to a label. This gives an error at runtime. With the code changed as follows, the upgrade wizard knows that the object is a label and hence upgrades the Caption property to text property correctly and the code executes correctly in Visual Basic .NET.<br><br> Dim objName1 As Label Set objName1 = Me.lblCodeSnippet6 ObjName1.Caption = "Some New Value" " Use explicit type conversions wherever possible to make the code readable. This will also help reduce the number of warnings that get issued when the code gets migrated to Visual Basic .NET using the upgrade wizard: Dim A As String Dim B As Integer Dim C As String A = "1" B = 2 C = "3" MsgBox (A + B) 'Result is 3 MsgBox (A + C) 'Result is 13 MsgBox (B + C) 'Result is 5 In this code snippet the addition is done in the context of the type of the variables being used as operands. For example, if the addition operation involves an integer, the addition operation is numeric and produces a numeric result.<br><br> If the both the operands contain strings, the operation is string concatenation and results in a string. However, as a matter of good programming practice it is recommended to explicitly type the variables by using functions such as CInt or CStr : Dim A As String Dim B As Integer Dim C As String A = "1" B = 2 C = "3" MsgBox (A + CStr(B)) 'Result is 12 MsgBox (A + C) 'Result is 13 MsgBox (CStr(B) + C) 'Result is 23 In this case, the function CStr is explicitly used to mark that the additions are supposed to be string additions. " Make parameters to functions and procedures ByRef or ByVal .<br><br> Visual Basic 6.0 allows the parameters to be defined as ByRef or ByVal . If the parameter is not marked explicitly as ByVal or ByRef , the system determines whether it becomes ByRef or ByVal . If the parameter is of an intrinsic type ( Integer , Long , Boolean , String, and so on), it is assumed to be the default ByRef ; otherwise the parameter is assumed to be ByVal .<br><br> In Visual Basic .NET, all parameters are ByVal by default. So the subroutine or function has to be explicitly declared as ByRef to get it to change a parameter. " Default properties.<br><br> In Visual Basic 6.0, we use the default properties of objects while programming. For example, a TextBox control has a default property called Text . The following code shows that the TextBox control can be used directly in the method call and its default property would be used.<br><br> In the code, a TextBox named Text1 is already placed on a form. An instance of an object is created and the instance of Text1 is assigned to the object. Dim objTextBox As Object Set objTextBox = Me.Text1 MsgBox objTextBox Before migration this code should be changed as follows: Dim objTextBox As TextBox Set objTextBox = Me.Text1 MsgBox objTextBox.Text " Database applications.<br><br> ADO is still supported in Visual Basic .NET. However, RDO and DAO are not supported. So it is recommended to write database applications using ADO.<br><br> If the Visual Basic 6.0 application has been written using DAO or RDO, these technologies will work well in Visual Basic .NET only if data binding has not been used. For data binding and data environment, it is recommended to use ADO in the Visual Basic 6.0 application. DAO and RDO data binding is not supported in Visual Basic .NET.<br><br> " Add business logic to components and classes. It is possible that in a Visual Basic 6.0 application the business logic has been written along with the presentation logic. Because Visual Basic .NET has introduced new features such as Windows Forms and Web Forms, the Visual Basic 6.0 application creates problems during the migration to Visual Basic .NET.<br><br> Because it is easier to migrate classes of Visual Basic 6.0 into Visual Basic .NET than the modules, it is recommended that all business logic be written in the form of components and classes. " Use Date data type instead of double for manipulating dates: Dim dtTomorrow As Date Dim dtToday As Double dtTomorrow = Date dtToday = dtTomorrow dtTomorrow = #12/31/2002# MsgBox (DateDiff("d", dtToday, dtTomorrow)) In Visual Basic, Date type variables could be used in the form of doubles. The upgraded code will work in Visual Basic .NET.<br><br> However, functions for converting the double value into date values get called for converting double values into dates . This may add a bit of processing overhead. This can be avoided altogether by using a Date type variable in Visual Basic 6.0.<br><br> In the preceding code, variable dtToday can be declared as Date as follows: Dim dtTomorrow As Date Dim dtToday As Date dtTomorrow = Date dtToday = dtTomorrow dtTomorrow = #12/31/2002# MsgBox (DateDiff("d", dtToday, dtTomorrow)) Upgrade Wizard in Visual Studio .NET Visual Studio .NET is the new development environment for developing Microsoft .NET applications. Because Visual Basic has undergone lots of changes to its syntax and many new features have been added to the language, the Visual Studio .NET IDE contains an upgrade wizard, which helps in upgrading Visual Basic applications to Visual Basic .NET. This section covers details about the upgrade wizard.<br><br> Upgrading a Visual Basic 6.0 Project The upgrade wizard is used to upgrade Visual Basic 6.0 applications into a Visual Basic .NET application. Applications that have been developed using earlier versions of Visual Basic (that is, versions lower than Visual Basic 6.0), have to be upgraded to Visual Basic 6.0 before using the upgrade wizard. The most important thing about this upgrade wizard is that it leaves the original Visual Basic 6.0 project untouched and creates a new Visual Basic .NET project.<br><br> If a Visual Basic 6.0 application is opened in Visual Studio .NET, the upgrade wizard is invoked automatically as seen in Figure 3-2 . As shown, the upgrade wizard creates a new Visual Basic .NET project, which will be the upgraded version of the Visual Basic application being opened. The wizard will copy each file from the original project into the new project and make the necessary modifications to make it a Visual Basic .NET application.<br><br> Figure 3-2 . First screen when the upgrade wizard is opened in Visual Studio .NET. On clicking Next, the screen shown in Figure 3-3 is displayed.<br><br> As shown, the upgrade wizard automatically detects the type of the Visual Basic application to be converted. If the application is a DLL or custom library, the checkbox Generate default interfaces for all public classes is enabled. If this checkbox is not checked, interfaces will be generated only for those classes that have been implemented by other classes in project.<br><br> If the checkbox is checked, interfaces are generated for all the classes whether they have been implemented by other classes or not. Figure 3-3 . Selecting the project type for the upgrade wizard.<br><br> On clicking Next, we get Figure 3-4 . In this screen, the location of the Visual Basic .NET project can be specified. If the directory does not exist, it is created and a Visual Basic .NET project is created in the new directory.<br><br> On clicking Next, the screen obtained is as shown in Figure 3-5 . On clicking Next in the following screen the update process is started as shown in Figure 3-6 . Figure 3-4 .<br><br> Specifying location for creating the upgraded Visual Basic .NET application. Figure 3-5 . Ready to Upgrade Screen.<br><br> Figure 3-6 . Upgrade Engine is invoked. Once the upgrade wizard has upgraded the application, the tool prepares an upgrade report that lists all the steps that the wizard has completed and what steps are remaining for the developer to complete the upgrade.<br><br> Upgrade Report When the upgrade tool has finished upgrading the application, it prepares an upgrade report that can be seen in the solution explorer window of Visual Studio .NET. The file name is _UpgradeReport.htm . Double-click the file name so that it will be opened in the Visual Studio .NET browser window.<br><br> Figure 3-7 shows the upgrade report. Figure 3-7 . Upgrade report generated by the upgrade wizard in Visual Studio .NET.<br><br> As seen in Figure 3-7 , the upgrade report shows the upgrade details. Specifically it shows the new file name (in the Visual Basic .NET project), the old file name (in Visual Basic), and the file type (whether is a form, a class, or so on). It also shows the status of the upgrade (i.e., whether the file was upgraded or whether it had any errors or warnings) and gives the error and warning counts for individual files.<br><br> Clicking on the + sign next to the file name will give the details of the file, as shown in Figure 3-8 . The settings used during the upgrade process are also listed in the upgrade report and a log file is created. Figure 3-8 shows the issues (if any) for a specific file.<br><br> Figure 3-8 . Upgrade issues for a specified file. Now let us look at an example of the upgrade wizard in which there were errors and warnings during the upgrade process.<br><br> Figure 3-9 shows the screen. As shown, when Visual Basic 6.0 was upgraded, there were a few errors. The report shows the upgrade issues.<br><br> Specifically the report lists the severity of the error, the location of the error, and the object, and the property on which the error occurred and gives a description of the error. Figure 3-9 . Upgrade report showing a list of files with errors and warnings.<br><br> COMPILE ERRORS Compile errors occur when the upgraded code cannot be compiled in Visual Basic .NET because some property or object is not supported in Microsoft .NET Framework. Any issues that fall under compile errors in an upgrade report need to be corrected by the developer; otherwise the application cannot be executed. The upgrade wizard will insert upgrade issues in case of compile errors.<br><br> Following are some of the cases that cause compilation errors during the upgrade: " The Visual Basic 6.0 application uses ListIndex property of ListBox or ComboBox control. " The Visual Basic 6.0 application uses DrawStyle property of PictureBox control. " Functions like VarPtr , StrPtr , ObjPtr , MidB , and ChrB and statements like GoSub have been used in the Visual Basic 6.0 application.<br><br> " The Visual Basic 6.0 application is trying to set a read-only property of particular control. DESIGN ERRORS When design errors occur, the upgraded code can be compiled and executed in Visual Basic .NET. This error indicates that a property that can be set at design time in Visual Basic 6.0 cannot be set then in Visual Basic .NET.<br><br> Design errors generally apply to forms and controls. Some causes for design errors are as follows: " A particular property of one control has a new behavior in Visual Basic .NET. Following are some of the cases that cause these errors: o TextBox.TextLength property o Form.Picture property o Form.Unload event o ComboBox.Change event " Raster fonts are used on the Visual Basic form.<br><br> " A particular property or method of Visual Basic 6.0 has been removed in Visual Basic .NET Warnings The upgraded code can be compiled and executed in Visual Basic .NET. The upgrade wizard issues a warning when some property that could be set in Visual Basic exhibits a changed behavior in Visual Basic .NET. Any warnings should be examined to make sure that they do not affect application behavior.<br><br> Following are some of the cases when the upgrade wizard issues warnings: " If arrays have been declared with New keyword. " If an array doesn't have a zero lower bound. " If Visual Basic application has used Null or IsNull because Null is not supported in Visual Basic .NET.<br><br> " In Visual Basic .NET some of the properties enumerators of Visual Basic 6.0 have changed. Therefore, an upgrade wizard shows a warning that the value for particular property could not be resolved. There are instances when no error is listed in the upgrade report, but the application fails to execute in the Microsoft .NET environment.<br><br> However, such cases are very infrequent. As Figure 3-9 indicates, the descriptions of the errors and warning are actually hyperlinks, which provide links to the specified materials in Microsoft .NET documentation. For example, clicking on the third description in the figure will lead to the screen shown in Figure 3-10 .<br><br> This screen gives an indication of why there was an error in upgrading the Visual Basic application to Visual Basic .NET. It also gives a pointer and a detailed explanation to the developer on how to solve the error. Figure 3-10 .<br><br> MSDN documentation for the specified error/warning. In addition to the upgrade report, the wizard also puts comments in the upgraded code to indicate that there were issues. The following code snippet shows a Visual Basic .NET code that was generated by the upgrade wizard: Private Sub Form1_Click(ByVal eventSender As_ System.Object, ByVal eventArgs As System.EventArgs)_ Handles MyBase.Click 'UPGRADE_ISSUE: PictureBox property Picture1.FillColor was not upgraded.<br><br> Click for more: 'ms-help://MS.VSCC /commoner/redir/redirect.htm?keyword="vbup2064"' Picture1.FillColor = System.Drawing.ColorTranslator._ ToOle (System.Drawing.Color.Red) 'UPGRADE_ISSUE: PictureBox property Picture1.FillStyle was not upgraded. Click for more: 'ms-help: // MS.VSCC / commoner/redir/ redirect.htm?keyword ="vbup2064"' Picture1.FillStyle = 0 'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSCC/ commoner/redir/redirect.htm?keyword="vbup2064"' Picture1.Circle (VB6.PixelsToTwipsX(Picture1.Width)/_ 2, VB6.PixelsToTwipsY(Picture1.Height) / 2),_ VB6.PixelsToTwipsY(Picture1.Height) / 2 End Sub In this code, the upgrade wizard has marked lines of code with an upgrade issue warning.<br><br> The wizard also provides a link to MSDN documentation on more specifics of the upgrade issues. Performance of the Upgrade Wizard For small projects, the upgrade wizard works very quickly. For Visual Basic 6.0 projects that use COM and ActiveX components, the upgrade wizard updates a copy of the main Visual Basic 6.0 project.<br><br> The original copy of the project is left unchanged. In addition, it creates a COM interoperability layer to enable the new Visual Basic .NET application to interact with the original COM components. This means that when you upgrade Visual Basic 6.0 projects, all the referenced COM and ActiveX controls are not upgraded automatically.<br><br> Instead, it uses the feature of interoperability mechanism provided by the Microsoft .NET Framework to interact with these COM and ActiveX components. The pre-migration recommendations provided in this book have been suggested to improve the performance of upgrade wizard. The Visual Basic 6.0 code needs to can be changed as per pre-migration recommendations before upgrading it using the upgrade wizard.<br><br> Summary This chapter has covered the phases required for migrating Visual Basic 6.0 applications to Visual Basic .NET. The first phase for the migration is the assessment phase that helps the developers decide whether to migrate the application to .NET or to leave it in Visual Basic 6.0. Once the decision to migrate the application has been made, premigration changes are done in the Visual Basic 6.0 application.<br><br> The chapter has covered the key differences in the language and syntax of Visual Basic 6.0 and Visual Basic .NET. It has also covered the differences in the data types of these two languages. There are certain changes that can be made in the Visual Basic 6.0 application to smooth the upgrade to Visual Basic .NET.<br><br> These changes are known as pre-migration recommendations. They are not mandatory, and developers can directly upgrade their Visual Basic 6.0 applications using the upgrade wizard. However, if developers follow the premigration recommendations, the actual changes that need to be done to the upgraded Visual Basic .NET application are reduced to some extent.<br><br> After the pre-migration changes, the application is upgraded to Visual Basic .NET with the help of the upgrade wizard. Microsoft has provided an upgrade wizard as part of the Visual Studio .NET IDE. This wizard leaves a copy of the original Visual Basic 6.0 application and upgrades the application into Visual Basic .NET application in a new directory.<br><br> The wizard makes all the necessary syntax changes. The Visual Basic 6.0 Forms and controls are upgraded into the proper Windows Forms and controls in Visual Basic .NET. However, there are many features in Visual Basic 6.0 applications that are not upgraded by the wizard.<br><br> This is primarily due to the very significant differences in the syntax and the architecture between Visual Basic 6.0 and Visual Basic .NET. The upgrade wizard provides a good starting point for the migration because it upgrades the basic features of the Visual Basic 6.0 application. Post-migration changes have to be done in Visual Basic .NET code to make it work in the Microsoft .NET environment based on the issues identified in the upgrade report.<br><br> These changes are covered in detail in the next chapter. © 2008 Pearson Education, Inc. Informit.<br><br> All rights reserved. 800 East 96th Street Indianapolis, Indiana 46240

less

Copyright © 2010 beepdf.com. All rights reserved.