A Visual Basic .NET program has three libraries in reach. The .NET base class library (System, System.IO, System.Linq, System.Collections.Generic and the rest) is the same as for every .NET language. The Visual Basic runtime (the Microsoft.VisualBasic namespace) adds the functions and constants of earlier Visual Basic versions. The My namespace is a set of shortcuts that the project generates. The Microsoft.VisualBasic namespace is imported into every project by default.
The Microsoft.VisualBasic runtime
The functions are shared members of modules, so they are called by their bare names. The viewer colours the ones below as library functions (when followed by a bracket in .NET code).
| Module | Examples |
|---|---|
Strings | Asc AscW Chr ChrW Filter Format FormatCurrency FormatDateTime FormatNumber FormatPercent InStr InStrRev Join LCase Left Len LTrim Mid Replace Right RTrim Space Split StrComp StrConv StrReverse Trim UCase |
Conversion | Fix Hex Int Oct Str Val CTypeDynamic |
Information | IsArray IsDate IsDBNull IsError IsNothing IsNumeric IsReference LBound UBound TypeName VarType RGB QBColor, and the Err object |
Interaction | MsgBox InputBox Shell Environ CallByName Choose IIf Switch GetObject CreateObject AppActivate Beep GetSetting SaveSetting DeleteSetting |
DateAndTime | Now Today TimeOfDay Timer DateAdd DateDiff DatePart DateSerial DateValue TimeSerial TimeValue Day Month Year Hour Minute Second Weekday WeekdayName MonthName |
FileSystem | FileOpen FileClose Print PrintLine WriteLine Input LineInput EOF LOF Loc Seek FreeFile Dir FileLen FileCopy Kill MkDir RmDir CurDir ChDir ChDrive GetAttr SetAttr (the VB6 file statements as functions) |
Financial | DDB FV IPmt IRR MIRR NPer NPV Pmt PPmt PV Rate SLN SYD |
VBMath | Rnd Randomize |
Constants, ControlChars | vbCrLf vbCr vbLf vbNewLine vbTab vbBack vbFormFeed vbNullChar vbNullString vbObjectError vbVerticalTab; the MsgBox values (vbOKOnly vbYesNo vbQuestion vbExclamation …), the comparison values (vbBinaryCompare vbTextCompare), the day and date-format values, the variant-type values; and the same characters as ControlChars.CrLf, ControlChars.Tab … |
Collection | the VB6-style collection class: Add(item, key, before, after), Item, Remove, Count, indexed from 1 |
Dim s = "Hello, World"
Dim a = Left(s, 5) & Right(s, 5) & Mid(s, 8, 3) & UCase(s) & Trim(" x ") & Len(s)
Dim b = InStr(s, "World") & Replace(s, "World", "VB") & Join(Split("a,b,c", ","), "-")
Dim c = Format(1234.5, "N2") & Format(Now, "yyyy-MM-dd") & FormatNumber(2.5, 1)
Dim d = Val("12abc") & Hex(255) & Int(-2.5) & Fix(-2.5) & Asc("A") & Chr(66)
Dim e = IsNumeric("12") & IsDate("2020-01-01") & TypeName(s)
Dim f = DateAdd(DateInterval.Day, 1, Now).Year & DateDiff(DateInterval.Year, #1/1/2000#, Now)
Dim g = vbCrLf & vbTab & vbNullString & CStr(Choose(2, "a", "b"))
Dim col As New Collection
col.Add("x", "key")
FileOpen(1, "x.txt", OpenMode.Output)
PrintLine(1, "text")
FileClose(1)
Int(-2.5) is −3 and Fix(-2.5) is −2.The string functions number characters from 1 (Mid("abc", 1, 1) is "a"), unlike the .NET String methods, which count from 0. Most of them have .NET equivalents (Substring, ToUpper, IndexOf) that are preferred in new code.
The My namespace
My is a namespace of shortcuts to the commonly used parts of the library (version 8). Some members are generated into the project (My.Settings from the settings file, My.Resources from the resources, My.Forms as default instances of the forms, My.WebServices) and some come from the library: My.Computer (FileSystem, Clipboard, Network, Audio, Registry, Info, Clock, Keyboard, Mouse, Ports, Screen), My.Application (Info, CommandLineArgs, Log, Culture) and My.User. Which members exist depends on the project type. A .NET 10 console project, built here with the SDK, did not have My.Computer, My.Application or My.User (the build said the types behind them are not defined), so code that uses them belongs in a Windows Forms or WPF project or in .NET Framework code.
The base class library
The types most used from Visual Basic are Console, String and System.Text.StringBuilder, Math, DateTime and TimeSpan, Convert, System.IO.File, Path and Directory, the collections of System.Collections.Generic (List(Of T), Dictionary(Of K, V), HashSet(Of T), Queue(Of T), Stack(Of T)) and System.Linq, System.Threading.Tasks and System.Xml.Linq. They are described by the .NET documentation, not here.
Not described
Known, not yet covered
- Reference: the signatures, defaults and edge cases of each runtime function, the full constant tables, the members of
My.Computerand the other My classes, and the application framework (startup, shutdown and unhandled-exception events) are documented in Microsoft’s library reference and are only listed here by name. - Reference: which project types (Windows Forms, WPF, ASP.NET, console on .NET Framework or .NET 5+) get which My members was observed only for one console project on .NET 10.
- Viewer: the library functions are coloured as a fixed list; a function that the list lacks, or a name of the list used as a variable, is not told apart from the text.
See also
- VB6 and VBA – where most of these functions come from
- Statements and control flow – statements that replaced the older file and error functions