Admit it, you do it to. You can’t remember the .NET way to do something, so you give-in and put Imports Microsoft.VisualBasic at the top of your .vb file. It’s time to stop the madness! Where there is help, there is hope.
As of recently, a co-worker explained to me the virtues of not using the old-school VB namespace in favor of learning the newer VB.NET way of doing things. One of the benefits is that VB.NET code is typically optimized to run faster… and we all love speed, don’t we? There were two places in particular that I kept resorting to the old VB way: date manipulation and formatting.
For example, here is the old VB way to add 7 days to the current date:
DateAdd(DateInterval.Day, 7, Date.Now)
And here is the VB.NET way:
Date.Now.AddDays(7)
That’s not so bad, is it? Now let’s look at formatting. Sure, there are quite a few ways to do the same thing, but here’s an example. If you want to format a date type variable in VB:
Format(Date.Now.ToString, "MM/dd/yyyy")
… or …
FormatDateTime(Date.Now, DateFormat.ShortDate)
And in VB.NET:
Date.Now.ToString("MM/dd/yyyy")
Actually, the VB.NET way seems easier! I wish I would’ve explored the VB.NET method sooner.
Here's some more handy conversions:
To subtract to dates:
Dim dblDateDiff As Double = datEndDate.Subtract(datStartDate).Days
So far, I’ve only come across one VB function (but I’m sure there are more) that I haven’t been able to find a VB.NET replacement for: vbTab. I found the replacement for vbCrLf is Environment.NewLine (one of the new VB.NET classes that is actually less convenient than its VB counterpart), but I have yet to find a way to put a simple tab space into a string. Not a huge problem, but odd in my opinion since that means I can’t completely lay the Visual Basic namespace to rest!
To help yourself cure your addiction to Visual Basic (the old-school kind), set the filter in the Microsoft Visual Studio.NET 2003 Documentation library to ‘.NET Framework’ instead of ‘Visual Basic’. I was under the impression that the ‘Visual Basic’ filter meant VB.NET, but it does not.