Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Visual Basic (.NET)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Comparative examples ==== The following simple examples compare VB and VB.NET syntax. They assume that the developer has created a form, placed a button on it and has associated the subroutines demonstrated in each example with the click [[event handler]] of the mentioned button. Each example creates a "Hello, World" message box after the button on the form is clicked. Visual Basic 6: <syntaxhighlight lang="vbscript"> Private Sub Command1_Click() MsgBox "Hello, World" End Sub </syntaxhighlight> VB.NET (MsgBox or MessageBox class can be used): <syntaxhighlight lang="vbnet"> Private Sub Button1_Click(sender As object, e As EventArgs) Handles Button1.Click MsgBox("Hello, World") 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 ({{code|Command1}}), an underscore ({{code|_}}), and the event's name ({{code|Click}}, hence {{code|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|2=vbnet|ByVal sender as Object}}, {{code|2=vbnet|ByVal e as EventArgs}} 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: <syntaxhighlight lang="vbscript"> Sub cmdClose_Click() Unload Me End Sub </syntaxhighlight> VB.NET: <syntaxhighlight lang="vbnet"> Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click Close() End Sub </syntaxhighlight> The 'cmd' prefix is replaced by the 'btn' prefix, conforming to the new convention previously mentioned.{{Which|date=November 2014}} Visual Basic 6 did not provide common operator shortcuts. The following are equivalent: <!-- Visual Basic 6 did NOT have an opacity setting for forms or any other standard control --> Visual Basic 6: <syntaxhighlight lang="vbscript"> Sub Timer1_Timer() 'Reduces Form Height by one pixel per tick Me.Height = Me.Height - 1 End Sub </syntaxhighlight> VB.NET: <syntaxhighlight lang="vbnet"> Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Me.Height -= 1 End Sub </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)