From VB6 to VB.NET with the Microsoft.VisualBasic Namespace

Level:
Level2

A Few Words about the Microsoft.VisualBasic Namespace

In addition to "native .NET" methods for handling strings, dates, file input/output and the like, the .NET framework includes the Microsoft.VisualBasic namespace, which contains functions that will look very familiar to classic VB (pre-.NET) programmers. In nearly all cases, the MS.VB namespace functions behave identically to their pre-.NET (i.e., VB6) counterparts. Whether or not the Microsoft.VisualBasic namespace functions should be used is the subject of some debate.

"Purists" will argue that only the native .NET methods should be used because they are common across all .NET languages (C# for example), that the MS.VB namespace functions may not be supported in future versions (although Microsoft insiders have actually indicated the opposite), and for more subjective reasons (such as a desire to make a clean break with the past, or the attitude that these "legacy" functions are simply not the ".NET way").

On the other hand, it is no less efficient to use the MS.VB namespace functions (and in many cases they are more efficient), in some cases they provide functionality not easily replicated with native .NET methods, and from a productivity standpoint, they provide a comfort zone to those coming to .NET from a VB6 or VBA background.

Please note that the Microsoft.VisualBasic namespace should not be confused with the Microsoft.VisualBasic.Compatibility namespace, which is used primarily by upgrade tools and should NOT be used for development.

In the other articles on this site, where equivalent functionality exists in both the native .NET methods and the Microsoft.VisualBasic namespace, both methodologies will be presented without bias. Where applicable, the descriptions, syntax, and examples will be presented side-by-side for easy comparison.

The chart below provides a partial list of the items that will be covered in the articles to follow.

Microsoft.VisualBasic Namespace Function

VB.NET Method or Property

Basic I/O

MsgBox

MessageBox

String Handling

Len

Length

Mid, Left, Right

Substring

(n/a)

StartsWith, EndsWith

Instr

IndexOf

InstrRev

LastIndexOf

(n/a)

Contains

Replace

Replace

UCase

ToUpper

LCase

ToLower

LTrim

TrimStart

RTrim

TrimEnd

Trim

Trim

(n/a)

String.Concat

(n/a)

Insert

(n/a)

Remove

(n/a)

PadLeft, PadRight

(n/a)

New String (equivalent to the pre-.NET "String" function to create a string of repeating characters)

StrReverse

(n/a)

Space

(n/a)

Asc, Chr

(n/a)

Split

Split

Join

String.Join

Filter

(n/a)

Date Handling

DateAdd

AddDays, AddMonths, AddYears, AddHours, AddMinutes, AddSeconds

DateDiff

TimeSpan variable = Date1 – Date2

DatePart, Month(datevar), Day(datevar), Year(datevar), Hour(datevar), Minute(datevar), Second(datevar)

datevar. Month, datevar.Day, datevar.Year, datevar.Hour, datevar.Minute, datevar.Second

This article was original written by The VB Programmer.

If you enjoyed this post, subscribe for updates (it's free)