Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

The Date Bug in VB 6 to VB.NET Conversion

An interesting bug, but a better example

By Dan Mabbutt, About.com

An interesting difference in the way the Date data type is handled makes it possible to write a program that works just fine in Visual Basic 6 and converts with no errors to Visual Basic .NET. But then it crashes with a runtime error in VB.NET. And it's all due to the ... well ... "sloppy" way that code could be written in VB 6 and Microsoft's efforts to clean things up in VB.NET.

All it takes to demonstrate the bug is a VB 6 program that reads a date from a Textbox and displays the same date in a Listbox. You might have a routine like this if there is a requirement to pass either a predetermined range of dates or a default date to another routine. (In my example below, the default date is next Christmas!)

Here's the VB 6 code:

Private Sub ProcessDate_Click()

Dim theDate As Date

On Error GoTo DefaultDate

If DateInput <> "" Then
   theDate = DateInput.Text
Else
   theDate = "25/12/2006"
End If
GoTo EchoDate

DefaultDate:
   theDate = "25/12/2006"

EchoDate:
   DateEcho.Caption = _
   Format$(theDate, "MMMM d, yyyy")
End Sub

The extra code is to ensure that either an acceptable date or a default date is displayed. The illustration below shows what happens when a variety of entries are made:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

In this program, the error checking and GoTo logic (see OOP and GoTo - A tale of structured programming) are key parts of the process that make it work.

Let's now assume that management (They never understand the problems of the real world, they just issue policy statements.) has now decided that all VB 6 code will be upgraded to VB.NET. To invoke the VB.NET Upgrade Wizard, we simply open the VB 6 project in VB.NET. The program seems to convert flawlessly ... sort of. Here's the converted code:

Option Strict Off
Option Explicit On
Friend Class Form1
      Inherits System.Windows.Forms.Form
   Private Sub ProcessDate_Click( _
      ByVal eventSender As System.Object, _
      ByVal eventArgs As System.EventArgs) _
      Handles ProcessDate.Click
      Dim theDate As Date
      On Error GoTo DefaultDate
      If DateInput.Text <> "" Then
         theDate = CDate(DateInput.Text)
      Else
         theDate = CDate("25/12/2006")
      End If
      GoTo EchoDate
DefaultDate:
      theDate = CDate("25/12/2006")
EchoDate:
      DateEcho.Text = CStr(theDate)
   End Sub
End Class

The only changes are that the output date formatting, one of the features of the VB 6 program, has simply disappeared; and the type converstions, CDate and CStr, that were being done "behind the scenes" in VB 6 are now in the code.

The program even works ... sort of ... when a correct date is entered. But it crashes with a runtime error when any sort of invalid date is entered and the On Error condition doesn't help. The results of running the converted program can be seen in this illustration:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Why did VB 6 work but VB.NET does not? The real bottom line answer is that our original programmer didn't use the American date style of "month-day-year" date formatting. The rest of the world uses "day-month-year" and that's the way the default date was formatted in the VB 6 program:

theDate = "25/12/2006"

It works in VB 6 because VB 6, in addition to converting from String to Date data type, also checks the Region code and switches the month and day ... correcting the coding error that had been there from the beginning in VB 6 .

The bottom line is that VB 6 is like a permissive parent. It forgives your errors and cleans up after you. VB.NET is like a strict parent. Make a mistake and it just lets you fail.

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. The Date Bug in VB 6 to VB.NET Conversion

©2009 About.com, a part of The New York Times Company.

All rights reserved.