| You are here: | About>Computing & Technology>Visual Basic> Using VB.NET> The Date Bug in VB 6 to VB.NET Conversion |
![]() | Visual Basic |
The Date Bug in VB 6 to VB.NET ConversionAn interesting bug, but a better exampleAn 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() 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: -------- 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 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: -------- 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. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


