Visual Basic (.NET)
≈ 10 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Visual Basic (VB), originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language developed by Microsoft and implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 20021 as the successor to its original Visual Basic language, the last version of which was Visual Basic 6.0.
Although the ".NET" portion of the name was dropped in 2005, this article uses "Visual Basic [.NET]" to refer to all Visual Basic languages released since 2002, in order to distinguish between them and the classic Visual Basic. Along with C# and F#, it is one of the three main languages targeting the .NET ecosystem. Microsoft updated its VB language strategy on 6 February 2023, stating that VB is a stable language now and Microsoft will keep maintaining it, but will not add support for new workloads.
Microsoft's integrated development environment (IDE) for developing in Visual Basic is Visual Studio. Most Visual Studio editions are commercial; the only exceptions are Visual Studio Express and Visual Studio Community, which are freeware. In addition, the .NET Framework SDK includes a freeware command-line compiler called vbc.exe. Mono also includes a command-line VB.NET compiler. Visual Basic is often used in conjunction with the Windows Forms GUI library to make desktop apps for Windows.…
Visual Basic (VB), originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language developed by Microsoft and implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visual Basic language, the last version of which was Visual Basic 6.0.
Although the ".NET" portion of the name was dropped in 2005, this article uses "Visual Basic [.NET]" to refer to all Visual Basic languages released since 2002, in order to distinguish between them and the classic Visual Basic. Along with C# and F#, it is one of the three main languages targeting the .NET ecosystem. Microsoft updated its VB language strategy on 6 February 2023, stating that VB is a stable language now and Microsoft will keep maintaining it, but will not add support for new workloads.
Microsoft's integrated development environment (IDE) for developing in Visual Basic is Visual Studio. Most Visual Studio editions are commercial; the only exceptions are Visual Studio Express and Visual Studio Community, which are freeware. In addition, the .NET Framework SDK includes a freeware command-line compiler called vbc.exe. Mono also includes a command-line VB.NET compiler. Visual Basic is often used in conjunction with the Windows Forms GUI library to make desktop apps for Windows.
Programming for Windows Forms with Visual Basic involves dragging and dropping controls on a form using a GUI designer and writing corresponding code for each control. The equals sign ("=") is used in both assigning values to variables and in comparison. Round brackets (parentheses) are used with arrays, both to declare them and to get a value at a given index in one of them. Visual Basic uses round brackets to define the parameters of subroutines or functions.
A single quotation mark (') or the keyword REM , placed at the beginning of a line or after any number of space or tab characters at the beginning of a line, or after other code on a line, indicates that the (remainder of the) line is a comment. Module Module1 Sub Main() ' The classic "Hello, World!" demonstration program Console.WriteLine("Hello, World!") End Sub End Module It prints "Hello, World!" on a command-line window. Each line serves a specific purpose, as follows: Module Module1 This is a module definition.
Modules are a division of code, which can contain any kind of object, like constants or variables, functions or methods, or classes, but can not be instantiated as objects like classes and cannot inherit from other modules. Modules serve as containers of code that can be referenced from other parts of a program. It is common practice for a module and the code file which contains it to have the same name. However, this is not required, as a single code file may contain more than one module or class. Sub Main() This line defines a subroutine called "Main".
How it works
"Main" is the entry point, where the program begins execution. Console.WriteLine("Hello, world!") This line performs the actual task of writing the output. Console is a system object, representing a command-line interface (also known as a "console") and granting programmatic access to the operating system's standard streams. The program calls the Console method WriteLine, which causes the string passed to it to be displayed on the console.
Instead of Console.WriteLine, an alternative is MsgBox, which prints the message in a dialog box instead of a command-line window. Imports System.Console Module Program Sub Main() Dim rows As Integer ' Input validation.
Do Until Integer.TryParse(ReadLine("Enter a value for how many rows to be displayed: " & vbcrlf), rows) AndAlso rows >= 1 WriteLine("Allowed range is 1 and {0}", Integer.MaxValue) Loop ' Output of Floyd's Triangle Dim current As Integer = 1 Dim row As Integer Dim column As Integer For row = 1 To rows For column = 1 To row Write("{0,-2} ", current) current += 1 Next WriteLine() Next End Sub Like Console.ReadLine but takes a prompt string.
Function ReadLine(Optional prompt As String = Nothing) As String If prompt IsNot Nothing Then Write(prompt) End If Return Console.ReadLine() End Function End Module Private Sub Command1_Click() MsgBox "Hello, World" End Sub VB.NET (MsgBox or MessageBox class can be used): Private Sub Button1_Click(sender As object, e As EventArgs) Handles Button1.Click MsgBox("Hello, World") End Sub Both Visual Basic 6 and Visual Basic .NET automatically generate the Sub and End Sub statements when the corresponding button is double-clicked in design view.
Visual Basic .NET will also generate the necessary Class and End Class statements. The developer need only add the statement to display the "Hello, World" message box. All procedure calls must be made with parentheses in VB.NET, whereas in Visual Basic 6 there were different conventions for functions (parentheses required) and subs (no parentheses allowed, unless called using the keyword Call ). The names Command1 and Button1 are not obligatory. However, these are default names for a command button in Visual Basic 6 and VB.NET respectively.
In VB.NET, the Handles keyword is used to make the sub Button1_Click a handler for the Click event of the object Button1 . In Visual Basic 6, event handler subs must have a specific name consisting of the object's name ( ), an underscore ( ), and the event's name ( , hence ). There is a function called MessageBox.Show in the Microsoft.VisualBasic namespace which can be used (instead of MsgBox ) similarly to the corresponding function in Visual Basic 6.
Impact
There is a controversy about which function to use as a best practice (not only restricted to showing message boxes but also regarding other features of the Microsoft.VisualBasic namespace). Some programmers prefer to do things "the .NET way", since the Framework classes have more features and are less language-specific. Others argue that using language-specific features makes code more readable (for example, using int (C#) or Integer (VB.NET) instead of System.Int32 ). In Visual Basic 2008, the inclusion of , has become optional.
The following example demonstrates a difference between Visual Basic 6 and VB.NET. Both examples close the active window. Visual Basic 6: Sub cmdClose_Click() Unload Me End Sub VB.NET: Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click Close() End Sub The 'cmd' prefix is replaced by the 'btn' prefix, conforming to the new convention previously mentioned. Visual Basic 6 did not provide common operator shortcuts.
The following are equivalent: Visual Basic 6: Sub Timer1_Timer() 'Reduces Form Height by one pixel per tick Me.Height = Me.Height - 1 End Sub VB.NET: Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Me.Height -= 1 End Sub Requires a button called Button1. Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MsgBox("Hello world!", MsgBoxStyle.Information, "Hello world!") ' Show a message that says "Hello world!".
End Sub End Class Module Module1 Sub Main() Console.WriteLine("Hello world!") ' Write in the console "Hello world!" and start a new line. Console.ReadKey() ' The user must press any key before the application ends. End Sub End Module Requires a TextBox titled 'TextBox1' and a button called Button1.
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click CreateObject("Sapi.Spvoice").Speak(TextBox1.Text) End Sub End Class Module Module1 Private Voice = CreateObject("Sapi.Spvoice") Private Text As String Sub Main() Console.Write("Enter the text to speak: ") ' Say "Enter the text to speak: " Text = Console.ReadLine() ' The user must enter the text to speak. Voice.Speak(Text) ' Speak the text the user has entered.
End Sub End Module Easy access to certain areas of the .NET Framework that otherwise require significant code to access like using My.Form2.Text = " MainForm " rather than System.WindowsApplication1.Forms.Form2.text = " MainForm " Dynamically generated classes (e.g.
My.Forms) Improved VB-to-VB.NET converter A "using" keyword, simplifying the use of objects that require the Dispose pattern to free resources Just My Code feature, which hides (steps over) boilerplate code written by the Visual Studio .NET IDE and system library code during debugging Data Source binding, easing database client/server development To bridge the gaps between itself and other .NET languages, this version added: Generics Partial classes, a method of defining some parts of a class in one file and then adding more definitions later; particularly useful for integrating user code with auto-generated code Operator overloading and nullable types Support for unsigned integer data types commonly used in other languages Visual Basic 2005 introduced the IsNot operator that makes 'If X IsNot Y' equivalent to 'If Not X Is Y' .
It gained notoriety when it was found to be the subject of a Microsoft patent application.
Text derived from https://en.wikipedia.org/w/index.php?oldid=1368362271 under CC BY-SA 4.0; modifications have been made.
References
- ↩Wikipedia “Visual Basic (.NET)” release date (×1) · confidence 0.95 revision URL