== Syntax ==
{{Expand section|date=April 2014}}
Visual Basic uses [[Statement (computer science)|statements]] to specify actions. The most common statement is an expression statement, consisting of an [[Expression (computer science)|expression]] to be evaluated, on a single line. As part of that evaluation, [[subroutine|functions or subroutines]] may be [[System call|called]] and [[Variable (programming)|variables]] may be [[assignment (computer science)|assigned]] new values. To modify the normal sequential execution of statements, Visual Basic provides several control-flow statements identified by reserved keywords. [[Structured programming]] is supported by several constructs including two conditional execution constructs
(<code>If</code> ... <code>Then</code> ... <code>Else</code> ... <code>End If</code> and <code>Select Case</code> ... <code>Case</code> ... <code>End Select</code> )
and three iterative execution (loop) constructs
(<code>Do</code> ... <code>Loop</code>, <code>For</code> ... <code>To</code>, and <code>For Each</code>) .
The <code>For</code> ... <code>To</code> statement has separate initialisation and testing sections, both of which must be present. (See examples below.) The <code>For Each</code> statement steps through each value in a list.
The <code>For Each</code> statement steps through each value in a list.
In addition, in Visual Basic:
*# There is no unified way of defining blocks of statements. Instead, certain keywords, such as "If … Then" or "Sub" are interpreted as starters of sub-blocks of code and have matching termination keywords such as "End If" or "End Sub".
*# Statements are terminated either with a [[Colon (punctuation)|colon]] (":") or with the [[end of line]]. Multiple-line statements in Visual Basic are enabled with " _" at the end of each such line. The need for the underscore continuation character was largely removed in version 10 and later versions.<ref>{{cite web |url=https://msdn.microsoft.com/en-us/library/ff637436.aspx |title=New Features in Visual Basic 10 |date=June 3, 2010 |access-date=September 5, 2015 |archive-date=March 4, 2016 |archive-url=https://web.archive.org/web/20160304231731/https://msdn.microsoft.com/en-us/library/ff637436.aspx |url-status=live }}</ref>
*# The [[equals sign]] ("=") is used in both assigning values to variables and in comparison.
*# [[Parentheses|Round brackets]] (parentheses) are used with [[Array data structure|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 <code>REM</code>, placed at the beginning of a line or after any number of [[Space character|space]] or [[Tab character|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 (computer programming)|comment]].
=== Simple example ===
End Sub
</syntaxhighlight>
*# Both Visual Basic 6 and Visual Basic .NET automatically generate the <code>Sub</code> and <code>End Sub</code> statements when the corresponding button is double-clicked in design view. Visual Basic .NET will also generate the necessary <code>Class</code> and <code>End Class</code> 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 <code>Call</code>).
*# The names <code>Command1</code> and <code>Button1</code> are not obligatory. However, these are default names for a command button in Visual Basic 6 and VB.NET respectively.
*# In VB.NET, the <code>Handles</code> keyword is used to make the sub <code>Button1_Click</code> a handler for the <code>Click</code> event of the object <code>Button1</code>. In Visual Basic 6, event handler subs must have a specific name consisting of the object's name ("Command1"), an underscore ("_"), and the event's name ("Click", hence "Command1_Click").
*# There is a function called <code>MessageBox.Show</code> in the <code>Microsoft.VisualBasic</code> namespace which can be used (instead of <code>MsgBox</code>) similarly to the corresponding function in Visual Basic 6. There is a controversy<ref>{{cite web|url=https://www.microsoft.com/en-us/download/details.aspx?id=55979|title=Visual Studio 2003 Retired Technical documentation|website=Microsoft Download Center|access-date=July 24, 2018|archive-date=December 30, 2014|archive-url=https://web.archive.org/web/20141230014657/http://msdn.microsoft.com/en-us/library/aa291820(VS.71).aspx|url-status=live}}</ref> about which function to use as a best practice (not only restricted to showing message boxes but also regarding other features of the <code>Microsoft.VisualBasic</code> 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 <code>int</code> (C#) or <code>Integer</code> (VB.NET) instead of <code>System.Int32</code>).
*# In Visual Basic 2008, the inclusion of <code>ByVal sender as Object, ByVal e as EventArgs</code> has become optional.
The following example demonstrates a difference between Visual Basic 6 and VB.NET. Both examples close the [[active window]].
For this release, Microsoft added many features intended to reinforce Visual Basic .NET's focus as a [[rapid application development]] platform and further differentiate it from [[C Sharp (programming language)|C#]]., including:
*# ''Edit and Continue'' feature{{elucidate|date=February 2014}}
*# Design-time expression evaluation{{Elucidate|date=February 2014}}
*# A pseudo-[[namespace]] called "My", which provides:<ref>{{cite web|url = http://msdn.microsoft.com/en-us/magazine/cc163680.aspx|title = Navigate The .NET Framework And Your Projects With The My Namespace|website = MSDN Magazine Visual Studio 2005 Guided Tour 2006|publisher = [[Microsoft]]|first = Duncan|last = Mackenzie|year = 2006|access-date = February 6, 2014|archive-date = February 15, 2014|archive-url = https://web.archive.org/web/20140215053141/http://msdn.microsoft.com/en-us/magazine/cc163680.aspx|url-status = live}}</ref><ref>{{cite web|url = http://msdn.microsoft.com/en-us/library/ms379610.aspx|title = My.Internals: Examining the Visual Basic My Feature|first = Tyler|last = Whitney|date = November 2005|website = [[MSDN]]|publisher = [[Microsoft]]|access-date = February 6, 2014|archive-date = June 14, 2012|archive-url = https://web.archive.org/web/20120614025325/http://msdn.microsoft.com/en-us/library/ms379610.aspx|url-status = live}}</ref>
**## Easy access to certain areas of the .NET Framework that otherwise require significant code to access like using <syntaxhighlight lang="vbnet" inline="">My.Form2.Text = " MainForm "</syntaxhighlight> rather than <syntaxhighlight lang="vbnet" inline="">System.WindowsApplication1.Forms.Form2.text = " MainForm "</syntaxhighlight>
**## Dynamically generated classes (e.g. ''My.Forms'')
*# Improved VB-to-VB.NET converter<ref>{{cite web|url=http://msdn2.microsoft.com/en-us/library/ms379614.aspx|title=What's New with the Visual Basic Upgrade Wizard in Visual Basic 2005|website=msdn2.microsoft.com|access-date=January 29, 2008|archive-date=April 6, 2008|archive-url=https://web.archive.org/web/20080406035326/http://msdn2.microsoft.com/en-us/library/ms379614.aspx|url-status=live}}</ref>
*# A "using" keyword, simplifying the use of objects that require the Dispose [[Design pattern (computer science)|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:
*# [[Generic programming|Generics]]<ref>{{cite web|url=http://msdn2.microsoft.com/en-us/library/ms379608.aspx|title=Defining and Using Generics in Visual Basic 2005|website=msdn2.microsoft.com|access-date=January 29, 2008|archive-date=April 23, 2008|archive-url=https://web.archive.org/web/20080423004351/http://msdn2.microsoft.com/en-us/library/ms379608.aspx|url-status=live}}</ref>
*# [[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 type]]s<ref>{{cite web|url=http://msdn2.microsoft.com/en-us/library/ms379613.aspx|title=Operator Overloading in Visual Basic 2005|website=msdn2.microsoft.com|access-date=January 29, 2008|archive-date=April 23, 2008|archive-url=https://web.archive.org/web/20080423001343/http://msdn2.microsoft.com/en-us/library/ms379613.aspx|url-status=live}}</ref>
*# Support for [[integer (computer science)|unsigned integer]] data types commonly used in other languages
Visual Basic 2005 introduced the <code>IsNot</code> operator that makes <code>'If X IsNot Y'</code> equivalent to <code>'If Not X Is Y'</code>. It gained notoriety<ref>{{cite web |url=https://www.theregister.co.uk/2005/02/22/real_slams_ms_patent/ |title=Real Software slams MS IsNot patent application |last=Sherriff |first=Lucy |publisher=The Register |date=February 22, 2005 |access-date=April 6, 2009 |archive-date=August 3, 2009 |archive-url=https://web.archive.org/web/20090803204929/http://www.theregister.co.uk/2005/02/22/real_slams_ms_patent/ |url-status=live }}</ref> when it was found to be the subject of a Microsoft patent application.<ref>{{cite web |url=http://www.eweek.com/article2/0,1759,1766949,00.asp |archive-url=https://archive.today/20120731141459/http://www.eweek.com/article2/0,1759,1766949,00.asp |url-status=dead |archive-date=July 31, 2012 |title=Real Software Slams Microsofts Patent Effort |last=Taft |first=Darryl K. |publisher=eWeek |date=February 21, 2005 |access-date=April 6, 2009 }}</ref><ref>{{cite web |url=http://appft1.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PG01&p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.html&r=1&f=G&l=50&s1=%2220040230959%22.PGNR.&OS=DN%2F20040230959&RS=DN%2F20040230959 |title=United States Patent Application: 20040230959 |work=Patent Application Full Text and Image Database |last1=Vick |first1=Paul A. Jr. |last2=Barsan |first2=Costica Corneliu |last3=Silver |first3=Amanda K. |date=May 14, 2003 |publisher=US Patent & Trademark Office |access-date=April 6, 2009 |archive-date=February 11, 2006 |archive-url=https://web.archive.org/web/20060211172531/http://appft1.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PG01&p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.html&r=1&f=G&l=50&s1=%2220040230959%22.PGNR.&OS=DN%2F20040230959&RS=DN%2F20040230959 |url-status=live }}</ref>
For this release, Microsoft added many features, including:
*# A true [[conditional operator]], "If(condition as boolean, truepart, falsepart)", to replace the "IIf" function.
*# [[Anonymous type]]s
*# Support for [[Language Integrated Query|LINQ]]
*# [[Anonymous function|Lambda expressions]]
*# [[XML Literals]]
*# [[Type inference|Type Inference]]
*# [[Extension method]]s
=== 2010 (VB 10.0) ===
=== 2012 (VB 11.0) ===
Visual Basic 2012 was released alongside [[.NET Framework 4.5]]. Major features introduced in this version include:{{Elucidate|date=February 2014}}
*# [[Asynchronous programming]] with "async" and "await" statements
*# Iterators
*# Call hierarchy
*# Caller information
*# "Global" keyword in "namespace" statements
=== 2013 (VB 12.0) ===
== See also ==
{{Portal|Free and open-source software|Computer programming}}
*# [[Microsoft Visual Studio Express]]
*# [[List of .NET libraries and frameworks]]
*# [[Comparison of C Sharp and Visual Basic .NET|Comparison of C# and Visual Basic .NET]]
*# [[Visual Basic for Applications]]
*# [[Microsoft Small Basic]]
*# [[Comparison of programming languages]]
== References ==
{{Wikibooks|Visual Basic .NET}}
{{wikiversity|VB.NET}}
*# {{Official website|https://learn.microsoft.com/en-us/dotnet/visual-basic/}}
*# [https://devblogs.microsoft.com/vbteam/ The Visual Basic Team Blog]
{{BASIC}}
|