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!
=== Comparison with the classic Visual Basic === {{Main|Comparison of Visual Basic and Visual Basic .NET}} Whether Visual Basic .NET should be considered as just another version of Visual Basic or a completely different language is a topic of debate. There are new additions to support new features, such as [[exception handling|structured exception handling]] and short-circuited expressions. Also, two important data-type changes occurred with the move to VB.NET: compared to Visual Basic 6, the <code>Integer</code> [[data type]] has been doubled in length from 16 bits to 32 bits, and the <code>Long</code> [[data type]] has been doubled in length from 32 bits to 64 bits. This is true for all versions of VB.NET. A 16-bit integer in all versions of VB.NET is now known as a <code>Short</code>. Similarly, the [[Windows Forms]] editor is very similar in style and function to the Visual Basic form editor. The things that ''have'' changed significantly are the semantics—from those of an object-based programming language running on a [[deterministic]], [[reference counting|reference-counted]] engine based on [[Component Object Model|COM]] to a fully [[object-oriented]] language backed by the [[.NET Framework]], which consists of a combination of the [[Common Language Runtime]] (a [[virtual machine]] using [[Garbage collection (computer science)#Generational GC (aka Ephemeral GC)|generational garbage collection]] and a [[just-in-time compilation]] engine) and a far larger [[class library]]. The increased breadth of the latter was also a problem that VB developers had to deal with when coming to the language, although this was somewhat addressed by the ''My'' feature in Visual Studio 2005. The changes altered many underlying assumptions about the "right" thing to do with respect to the performance and maintainability of applications. Some functions and libraries no longer exist; others are available, but not as efficient as the "native" .NET alternatives. Even if they compiled, most converted Visual Basic 6 applications required some level of [[refactoring]] to take full advantage of the .NET language. Microsoft provided documentation to cover changes in language syntax, debugging applications, deployment, and terminology.<ref>{{cite web|url = http://msdn.microsoft.com/en-us/vstudio/ms788233|title = Microsoft Visual Basic 6.0 Migration Resource Center|access-date = November 9, 2014|website = [[MSDN]]|publisher = [[Microsoft]]|archive-date = November 9, 2014|archive-url = https://web.archive.org/web/20141109140433/http://msdn.microsoft.com/en-us/vstudio/ms788233|url-status = live}}</ref> A popular trade book designed to ease the transition was [[Michael Halvorson|Michael Halvorson's]] ''Microsoft Visual Basic .NET Professional Step by Step'', published in 2002 by [[Microsoft Press]]. ==== 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)