Sunday, February 21, 2010

.NET: VB6 to VB.NET Migration TIPS


  1. First thing you must do is to Convert earlier version of VB code to VB6 .
  2. Add "Option Explicit" to all VB code behind forms. Because VB.NET does not support a variable declaration without a type declaration. So do not leave any variable declaration types as default variant type.
  3. Just keep in mind that all the controls in VB.NET are objects.
  4. Clean the multiple variable declarations per line. All variable declarations should be in deferent lines.
  5. Do not use (0 – Zero) to disable the timer, instead set Timer enabled = false.
  6. Make sure the code compile and runs before the conversion.
  7. Convert all arrays to Zero based.
  8. Migrate all your Data access code from DAO and RDO to ADO.
  9. Do not use defaults when accessing control properties. Change code that is using Text1 to read the text box value because Text1 in VB.NET represents textbox object.
  10. Rename any VB.NET keywords in the VB6 code.
  11. Form.Print doesn't exist in VB.NET
  12. Another feature missing from VB.NET is control arrays.
  13. VB6 is a 16-bit integer; in VB.NET, it is a 32-bit integer. The 16-bit equivalent in .NET is a Short,
  14. VB.NET requires a managed RCW class to wrap every ActiveX control or COM component it needs to use. Thankfully, the Upgrade Wizard creates these assemblies automatically, and modifies the imported code to use them.
  15. The ZOrder propertyis missing in VB.NET , the wizard will convert it, but add a message that the behavior has changed.
  16. An Integer in VB6 is a 16-bit integer; in VB.NET, it is a 32-bit integer. The 16-bit equivalent in .NET is a Short

  17. In .NET, control arrays aren't needed to handle multiple events. Instead, you can use the handles clause or the AddHandler statement to link up as many controls as you want to a single event handler. You can then use the sender parameter to interact with the control that fires the event.
    VB.NET code:
    Private Sub Highlight(sender As _ Object, e As MouseEventArgs) _
    Handles lblLine1.MouseMove, _ lblLine2.MouseMove
    Dim lbl As Label = _ CType(sender, Label)
    lbl.ForeColor = _ Color.RoyalBlue
    End Sub.

  18. Big change in Popup Menu's
    In classic VB you usually creates munu object and use built-in PopupMenu command to activate it:

    VB 6 code:
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then
    PopupMenu mnuOrderOptions
    End If
    End Sub

  19. In VB.NET, you can't use the same menu for a main menu and a context menu. Instead, you'll need to create a new ContextMenu object, and copy the ordinary menu information into it with the CloneMenu() method:
    VB .NET code:
    Dim mnuPopUp As New ContextMenu()
    Dim mnuItem As MenuItem
    Private Sub Form1_MouseDown (ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    If e.Button = Mouse _ Buttons.Right Then
    Me.ContextMenu.Show(Me, _ New Point(e.X, e.Y))
    End If
    End Sub
  20. An Internet project using Web Classes, ActiveX Documents, or DHTML: None of these development technologies is supported in .NET.
  21. A database project that uses data binding to other controls: These features can be upgraded under some circumstances, but will definitely fail with the DAO and RDO data access technologies.
  22. An ActiveX control or ActiveX DLL project: While you can create .NET equivalents of these COM-based types of programs, you will lose all of their existing COM features. If you have controls or components that are still being shared among numerous applications, it will probably be easier to use them in .NET or to make additional .NET versions, rather than trying to migrate them and replace the originals.
  23. ADO and ADO.NET objects models are totally different. Although it is certainly possible to use ADO within your Visual Basic .NET application, your mileage will vary as far as performance is concern. It is possible to preserve your existing investment in ADO while still taking advantage of .NET controls and performing such tasks as data binding and marshaling Recordsets through XML Web services.

  24. The classic ADO Recordset functionality has been split into smaller and more specialized objects: the DataSet and DataReader classes. Forward-only "firehose" Recordsets map directly to DataReaders, while disconnected Recordsets map best to DataSets. In essence, a DataSet contains collections of DataTables and DataRelations. The DataTable class conceptually corresponds closely to a database table, while the DataRelation class is used to specify relations between the DataTables.
  25. Following is the simple to convert your ADO RecordSet object to VB.NET DataTable object using OleDBDataAdaptor.
    Dim olead As New Data.OleDb.OleDbDataAdapter()
    Dim dt As New Data.DataTable()
    'We use the DataAdapter to fill a DataTable
    olead.Fill(dt, rs)
    'Now we bind the DataTable to the DataGrid
    Me.DataGrid1.DataSource = dt
    'This is cleanup. Always close your objects as soon as you are done with them.
    rs.Close()
    cn.Close()
  26. Calling classic VB COM components (unmanaged code libraries) from .NET managed code is known as a Runtime-Callable Wrapper. See the corresponding posting for more details. 
  27. Notice the changes happened to ADO Command object METHODS.
  • ExecuteReader : Execute a query and return a DataReader object.
  • ExecuteScalar : Execute a query and return a single value.
  • ExecuteXmlReader : Execute an XML query and return an Xml- Reader object.
  • ExecuteNonQuery : Execute a query with no results

2 comments:

takecare said...

HI Sir

I like the blog you posted. Please let me know if you had done such migrations earlier. One of my friend is looking for resources with knowledge and experience of VB6 to VB .NET migration. Please let me know if you are interested. Contact me at aruneshwar.reddy@hotmail.com

Macrosoft said...

Good guidelines for vb.net migration tips.
Thanks for sharing.

VB.Net Migration

Post a Comment