The following simple example demonstrates similarity in syntax between VB and VB.NET. Both examples pop up a message box saying "Hello, World" with an OK button.
Private Sub Command1_Click()
MsgBox "Hello, World"
End Sub
A VB.NET example, MsgBox or the MessageBox class can be used:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Msgbox("Hello, World")
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Hello, World")
End Sub
End Class
- Both Visual Basic 6 and Visual Basic .NET will automatically generate the
SubandEnd Substatements when the corresponding button is clicked in design view. Visual Basic .NET will also generate the necessaryClassandEnd Classstatements. The developer need only add the statement to display the "Hello, World" message box. - Note that all procedure calls must be made with parentheses in VB.NET, whereas in VB6 there were different conventions for functions (parentheses required) and subs (no parentheses allowed, unless called using the keyword
Call). - Also note that the names
Command1andButton1are not obligatory. However, these are default names for a command button in VB6 and VB.NET respectively. - In VB.NET, the
Handleskeyword is used to make the subButton1_Clicka handler for theClickevent of the objectButton1. In VB6, 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
MsgBoxin theMicrosoft.VisualBasicnamespace which can be used similarly to the corresponding function in VB6. 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 theMicrosoft.VisualBasicnamespace). 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, usingint(C#) orInteger(VB.NET) instead ofSystem.Int32). - In VB 2008, the inclusion of
ByVal sender as Object, ByVal e as EventArgshas become optional.
The following example demonstrates a difference between VB6 and VB.NET. Both examples close the active window.
Classic VB Example:
Sub cmdClose_Click()
Unload Me
End Sub
A VB.NET example:
Sub btnClose_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Note the 'cmd' prefix being replaced with the 'btn' prefix, conforming to the new convention previously mentioned.
Visual Basic 6 did not provide common operator shortcuts. The following are equivalent:
VB6 Example:
Sub Timer1_Timer()
Me.Height = Me.Height - 1
End Sub
VB.NET example:
Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Me.Height -= 1
End Sub
No comments:
Post a Comment